如何从ListWidget拖放到ComboBox [英] How to Drag and Drop from ListWidget onto ComboBox

查看:161
本文介绍了如何从ListWidget拖放到ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是能够将ListWidget项拖放到comboBox上。放置的项目应添加到组合框中。理想情况下,我们希望避免阅读listWidget的.currentItem()或.selectedItems()等技巧。

The goal is to be able to drag-and-drop ListWidget items onto the comboBox. The dropped items should be added to the combobox. Ideally we want to avoid any tricks with reading listWidget's .currentItem() or .selectedItems() and etc... Ideas?

from PyQt4 import QtGui, QtCore
import sys, os

class MyClass(object):
    def __init__(self):
        super(MyClass, self).__init__()
        self.name=None     
    def setName(self, arg):
        self.name=arg
    def getName(self):
        return self.name       

class DropableComboBox(QtGui.QComboBox):
    def __init__(self):
        self.model_mime_type = 'application/x-qabstractitemmodeldatalist'
        super(DropableComboBox, self).__init__()
        self.setAcceptDrops(True)

    def dragEnterEvent(self, event):
        if event.mimeData().hasFormat(self.model_mime_type) or event.mimeData().hasFormat('text/plain'):
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
            links = []
            for url in event.mimeData().urls():
                links.append(str(url.toLocalFile()))
            self.emit(QtCore.SIGNAL("dropped"), links)
        else:
            super(DropableComboBox, self).dropEvent(event)
            self.emit(QtCore.SIGNAL("dropped"))

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)

        self.listWidget = QtGui.QListWidget()
        self.listWidget.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
        self.listWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
        self.listWidget.currentItemChanged.connect(self.item_clicked)

        for i in range(3):
            my_item=QtGui.QListWidgetItem()
            name='ListWidget Item '+str(i)
            my_item.setText(name)
            self.listWidget.addItem(my_item)

            myObject=MyClass()
            myObject.setName(name) 

            my_item.setData(QtCore.Qt.UserRole, myObject)

        myBoxLayout.addWidget(self.listWidget)

        self.ComboBox = DropableComboBox()
        for i in range(3):
            self.ComboBox.addItem("Combobox Item " + str(i))

        self.ComboBox.currentIndexChanged.connect(self.combobox_selected)
        self.connect(self.ComboBox, QtCore.SIGNAL("dropped"), self.droppedOnCombobox)

        myBoxLayout.addWidget(self.ComboBox)

    def item_clicked(self, arg=None):
        print arg.data(QtCore.Qt.UserRole).toPyObject().getName()

    def combobox_selected(self, index):
        myObject=self.ComboBox.itemData(index).toPyObject()
        if hasattr(myObject, 'getName'): print myObject.getName()

    def droppedOnCombobox(self):
        print "Drop!"


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())


推荐答案

执行此操作的正确方法是使用 QDataStream 解压缩mimedata。但是,这似乎需要使用 QMap ,而PyQt中不提供此功能。因此,相反,可以通过为我们的肮脏工作获取代理模型,以稍微有点怪异的方式(或者应该是棘手的?)完成此操作:

The "proper" way to do this would be to unpack the mimedata using a QDataStream. However, this would seem to require the use of a QMap, which is not available in PyQt. So instead, it can be done in a slightly hacky (or should that be "tricky"?) way by getting a proxy model to the dirty work for us:

class DropableComboBox(QtGui.QComboBox):
    def __init__(self):
        super(DropableComboBox, self).__init__()
        self.model_mime_type = 'application/x-qabstractitemmodeldatalist'
        self.setAcceptDrops(True)
        self._proxymodel = QtGui.QStandardItemModel(self)

    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
            links = []
            for url in event.mimeData().urls():
                links.append(str(url.toLocalFile()))
            self.emit(QtCore.SIGNAL("dropped"), links)
        elif event.mimeData().hasFormat(self.model_mime_type):
            self._proxymodel.setRowCount(0)
            self._proxymodel.dropMimeData(
                event.mimeData(), QtCore.Qt.CopyAction,
                0, 0, QtCore.QModelIndex())
            for index in range(self._proxymodel.rowCount()):
                item = self._proxymodel.item(index, 0)                  
                self.addItem(item.text())
            # no point calling the base-class dropEvent here,
            # because it's a NO-OP in QComboBox
            self.emit(QtCore.SIGNAL("dropped"))

NB:

这将复制列表小部件中的项目,而不是移动 (您没有要求)。另外,如果要防止添加重复项,请使用 setDuplicatesEnabled 。而且,如果您要更改添加项目的方式,请使用 setInsertPolicy

This will copy the items from the list-widget, rather than moving them (which you didn't ask for). Also, if you want to prevent duplicates being added, use setDuplicatesEnabled. And if you want to alter how the items are added, use setInsertPolicy.

这篇关于如何从ListWidget拖放到ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆