我遇到了一些试图为自定义QListWidgetItem实现拖放的问题。以下是一些示例代码:
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_())
我的自定义类MyListWidgetItem有一个'data'字段,在我的真实程序中包含一些与该项相关的信息。如果您运行代码并尝试将项目从顶部列表拖放到底部,则一切正常,但如果您尝试将它们带回到顶部列表,则会出现此错误:
Traceback (most recent call last):
File "C:\Users\Massi\Desktop\t.py", line 23, in startDrag
t = [i.GetData() for i in self.selectedItems()]
AttributeError: 'QListWidgetItem' object has no attribute 'GetData'
似乎很清楚,默认的拖放行为忽略了列表项已经被子类化,所以我想知道哪个是最好的处理这种情况。任何帮助都非常感谢。 提前谢谢!
答案 0 :(得分:0)
我会将QListView
与QStandardItemModel
一起使用并使用QStandardItemModel.setItemPrototype(MyStandardItem())
并定义MyStandardItem
之类的内容:
class MyStandardItem(QtGui.QStandardItem):
def __init__(self, *__args):
super().__init__(*__args)
def clone(self):
return MyStandardItem()
答案 1 :(得分:0)
如果您添加一些调试信息,如下所示,您可能会发现“i”已经是MyListWidgetItem,因此这不应该是问题。
def startDrag(self, supportedActions):
drag = QtGui.QDrag(self)
# add the following 2 line debug information
for item in self.selectedItems():
print item.__class__.__name__
t = [i.GetData() for i in self.selectedItems() if hasattr(i, "GetData")]
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))
输出
MyListWidgetItem
问题是你不能将项类型强制转换为MyListWidgetItem,因为python不支持类型转换,所以你需要使用反射机制来调用GetData()方法,我刚试过代码,它的工作原理很好!