I have a GUI with two lists. Items can be dragged from one list to the other and vice versa. Also, I have “setSortingEnabled(True)” on both lists, so when an item is dragged from one list to the other, it automatically gets inserted in sorted order. The problem is that items can be dragged and rearranged within a single list. This breaks the sorted order. How can I disable internal drag-and-drop on a widget while still allowing drag-and-drop to/from other widgets?
from PySide import QtCore, QtGui
class TestSortedDragDrop(QtGui.QMainWindow):
def __init__(self, parent=None):
super(TestSortedDragDrop, self).__init__(parent)
self._centralWidget = QtGui.QWidget()
self.setCentralWidget(self._centralWidget)
layout = QtGui.QHBoxLayout(self._centralWidget)
self.list1 = QtGui.QListWidget()
self.list1.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
self.list1.setDefaultDropAction(QtCore.Qt.MoveAction)
self.list1.setSortingEnabled(True)
self.list2 = QtGui.QListWidget()
self.list2.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
self.list2.setDefaultDropAction(QtCore.Qt.MoveAction)
self.list2.setSortingEnabled(True)
layout.addWidget(self.list1)
layout.addWidget(self.list2)
l1 = ['car', 'bat', 'house', 'elm', 'desk']
l2 = ['rock', 'spoon', 'cup', 'book']
self.list1.addItems(sorted(l1))
self.list2.addItems(sorted(l2))