from PyQt4 import QtGui, QtCore
import sys, os
def __init__(self, label, data, parent=None):
self.data = data
def GetData(self):
return self.data
def __init__(self, type, parent=None):
super(MyListWidget, self).__init__(parent)
self.setAcceptDrops(True)
self.viewport().setAcceptDrops(True)
self.setDropIndicatorShown(True)
def startDrag(self, supportedActions):
t = [i.GetData() for i in self.selectedItems()]
mimeData = self.model().mimeData(self.selectedIndexes())
mimeData.setText(str(t))
drag.setMimeData(mimeData)
if drag.start(QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction:
for item in self.selectedItems():
self.takeItem(self.row(item))
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.ignore()
else:
event.accept()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls():
event.ignore()
else:
event.accept()
def dropEvent(self, event):
if event.mimeData().hasUrls():
event.ignore()
if isinstance(event.source(), MyListWidget):
event.setDropAction(QtCore.Qt.MoveAction)
super(MyListWidget, self).dropEvent(event)
else:
event.ignore()
def dropMimeData(self, index, mimedata, action):
super(MyListWidget, self).dropMimeData(index, mimedata, action)
return True
def __init__(self):
myQWidget.setLayout(myBoxLayout)
self.setCentralWidget(myQWidget)
self.listWidgetA = MyListWidget(self)
self.listWidgetB = MyListWidget(self)
for i in range(5):
listItemAInstance = MyListWidgetItem(str(i), i, parent=self.listWidgetA)
myBoxLayout.addWidget(self.listWidgetA)
myBoxLayout.addWidget(self.listWidgetB)
if __name__ == '__main__':
dialog_1 = Test()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())
from PyQt4 import QtGui, QtCore
import sys, os
class MyListWidgetItem(QtGui.QListWidgetItem):
def __init__(self, label, data, parent=None):
super(QtGui.QListWidgetItem, self).__init__(label, parent=parent)
self.data = data
def GetData(self):
return self.data
class MyListWidget(QtGui.QListWidget):
def __init__(self, type, parent=None):
super(MyListWidget, self).__init__(parent)
self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.setAcceptDrops(True)
self.viewport().setAcceptDrops(True)
self.setDropIndicatorShown(True)
def startDrag(self, supportedActions):
drag = QtGui.QDrag(self)
t = [i.GetData() for i in self.selectedItems()]
mimeData = self.model().mimeData(self.selectedIndexes())
mimeData.setText(str(t))
drag.setMimeData(mimeData)
if drag.start(QtCore.Qt.MoveAction) == QtCore.Qt.MoveAction:
for item in self.selectedItems():
self.takeItem(self.row(item))
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.ignore()
else:
event.accept()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls():
event.ignore()
else:
event.accept()
def dropEvent(self, event):
if event.mimeData().hasUrls():
event.ignore()
if isinstance(event.source(), MyListWidget):
event.setDropAction(QtCore.Qt.MoveAction)
super(MyListWidget, self).dropEvent(event)
else:
event.ignore()
def dropMimeData(self, index, mimedata, action):
super(MyListWidget, self).dropMimeData(index, mimedata, action)
return True
class Test(QtGui.QMainWindow):
def __init__(self):
super(QtGui.QMainWindow,self).__init__()
myQWidget = QtGui.QWidget()
myBoxLayout = QtGui.QVBoxLayout()
myQWidget.setLayout(myBoxLayout)
self.setCentralWidget(myQWidget)
self.listWidgetA = MyListWidget(self)
self.listWidgetB = MyListWidget(self)
for i in range(5):
listItemAInstance = MyListWidgetItem(str(i), i, parent=self.listWidgetA)
myBoxLayout.addWidget(self.listWidgetA)
myBoxLayout.addWidget(self.listWidgetB)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog_1 = Test()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Bookmarks