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:
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.