Reputation: 4718
I would like to use drag-and-drop to rearrange the items in a QComboBox. I tried this:
from PyQt4.QtGui import QApplication, QComboBox, QAbstractItemView
a = QApplication([''])
c = QComboBox()
c.addItem('a')
c.addItem('b')
c.addItem('c')
c.addItem('d')
view = c.view()
view.setDragDropMode(QAbstractItemView.InternalMove)
c.show()
c.raise_()
a.exec_()
However, dragging an item on top of another item deletes the dragged item -- I want that item to be moved above/below the drop location. Am I doing this incorrectly?
Upvotes: 1
Views: 474
Reputation: 120608
Each combo item needs to be disabled as a drop target by setting the appropriate item flags.
Here's one way to achieve that:
import sys
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication(sys.argv)
combo = QtGui.QComboBox()
model = QtGui.QStandardItemModel()
for text in 'One Two Three Four'.split():
item = QtGui.QStandardItem(text)
item.setFlags(item.flags() & ~QtCore.Qt.ItemIsDropEnabled)
model.appendRow(item)
combo.setModel(model)
combo.view().setDragDropMode(QtGui.QAbstractItemView.InternalMove)
combo.show()
app.exec_()
Upvotes: 1