gpt4 book ai didi

python - PyQt:如何在ListView的onDrop()之后获取listItem索引

转载 作者:行者123 更新时间:2023-12-01 05:16:53 25 4
gpt4 key购买 nike

此示例创建一个简单的对话框窗口,其中包含两个支持拖放的 listWidget(感谢 ekhumoro!)。在 dropEvent 上, DropOnA() 和 DropOnB() 函数打印出一条信息:这些项目来自哪个 listWidget,以及它们曾经拥有哪些索引。但我需要找到删除的 listItems 在被拖放到 QListWidget 接收器上后分配的索引。如果能解释如何实现这一点,我将不胜感激。

from PyQt4 import QtGui, QtCore
import sys, os

class MyClass(object):
def __init__(self):
super(MyClass, self).__init__()

class ThumbListWidget(QtGui.QListWidget):
_drag_info = []
def __init__(self, type, name, parent=None):
super(ThumbListWidget, self).__init__(parent)
self.setObjectName(name)
self.setIconSize(QtCore.QSize(124, 124))
self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.setAcceptDrops(True)

def startDrag(self, actions):
self._drag_info[:] = [str(self.objectName())]
for item in self.selectedItems():
self._drag_info.append(self.row(item))
super(ThumbListWidget, self).startDrag(actions)

def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
super(ThumbListWidget, self).dragEnterEvent(event)

def dragMoveEvent(self, event):
if event.mimeData().hasUrls():
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
else:
super(ThumbListWidget, self).dragMoveEvent(event)

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)
else:
# event.setDropAction(QtCore.Qt.MoveAction)
super(ThumbListWidget, self).dropEvent(event)
self.emit(QtCore.SIGNAL("dropped"), self._drag_info )


class Dialog_01(QtGui.QMainWindow):
def __init__(self):
super(QtGui.QMainWindow,self).__init__()
self.listItems={}

myQWidget = QtGui.QWidget()
myBoxLayout = QtGui.QVBoxLayout()
myQWidget.setLayout(myBoxLayout)
self.setCentralWidget(myQWidget)

self.listWidgetA = ThumbListWidget(self, 'MainTree')
self.listWidgetB = ThumbListWidget(self, 'SecondaryTree')

for i in range(7):
listItemA=QtGui.QListWidgetItem()
listItemA.setText('A'+'%04d'%i)
self.listWidgetA.addItem(listItemA)

myClassInstA=MyClass()
listItemA.setData(QtCore.Qt.UserRole, myClassInstA)

listItemB=QtGui.QListWidgetItem()
listItemB.setText('A'+'%04d'%i)
self.listWidgetB.addItem(listItemB)

myClassInstB=MyClass()
listItemB.setData(QtCore.Qt.UserRole, myClassInstB)

myBoxLayout.addWidget(self.listWidgetA)

myBoxLayout.addWidget(self.listWidgetB)
self.connect(self.listWidgetA, QtCore.SIGNAL("dropped"), self.droppedOnA)
self.connect(self.listWidgetB, QtCore.SIGNAL("dropped"), self.droppedOnB)


def droppedOnA(self, dropped_list):
print '\n\t dropped On MainTree', dropped_list,

def droppedOnB(self, dropped_list):
print '\n\t dropped On SecondaryTree', dropped_list


if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(720,480)
sys.exit(app.exec_())

最佳答案

未进行破坏测试,但尝试重新实现 rowsInserted并从那里发出拖动信息:

class ThumbListWidget(QtGui.QListWidget):
_drag_info = []
def __init__(self, type, name, parent=None):
...
self._dropping = False

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)
else:
# event.setDropAction(QtCore.Qt.MoveAction)
self._dropping = True
super(ThumbListWidget, self).dropEvent(event)
self._dropping = False

def rowsInserted(self, parent, start, end):
if self._dropping:
self._drag_info.append((start, end))
self.emit(QtCore.SIGNAL("dropped"), self._drag_info)
super(ThumbListWidget, self).rowsInserted(parent, start, end)

关于python - PyQt:如何在ListView的onDrop()之后获取listItem索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23002515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com