gpt4 book ai didi

python - pyqt5 覆盖 dropEvent python

转载 作者:太空宇宙 更新时间:2023-11-03 21:38:17 25 4
gpt4 key购买 nike

我正在尝试向小型应用程序添加拖放功能。从 QlistWidget 获取数据并将数据拖放到 QTableWidget 上。我应该重写 QTableWidget 的 dropEvent 以便在删除数据时添加一些其他函数。但我有麻烦,我想我无法获取从 ListWidget 获取的对象的 text() 。这是代码:

class Table(QtWidgets.QTableWidget):
def __init__(self,r,c, parent=None):
super().__init__(r,c,parent)
self.init_ui()

def init_ui(self):
self.setAcceptDrops(True)
self.setDragDropMode(QtWidgets.QAbstractItemView.DragDrop)

"""def dragMoveEvent(self, e):
e.setDropAction(QtCore.Qt.MoveAction)
e.accept()

def dragEnterEvent(self,e):
e.accept()"""



def dropEvent(self,e):
data = e.mimeData()
a=e.pos()
row = self.rowAt(a.y())
col = self.columnAt(a.x())
self.setItem(row,col,QtWidgets.QTableWidgetItem(data.text()))

print(row,col)
print(type(data.text()))
print(e.source())
x = data.text()
print(x)
e.accept()
`

最佳答案

通过拖放从QListWidget传输的数据不是通过text()给出的,因为一个项目有更多的信息由角色,此外您还可以拖动多个项目。数据使用MIME类型application/x-qabstractitemmodeldatalist传输,解决方案是对其进行解码,如下所示:

from PyQt5 import QtCore, QtWidgets


class TableWidget(QtWidgets.QTableWidget):
def __init__(self, r,c, parent=None):
super(TableWidget, self).__init__(r,c, parent)
self.setAcceptDrops(True)
self.setDragDropMode(QtWidgets.QAbstractItemView.DropOnly)


def dropEvent(self, event):
md = event.mimeData()
fmt = "application/x-qabstractitemmodeldatalist"
if md.hasFormat(fmt):
encoded = md.data(fmt)
stream = QtCore.QDataStream(encoded, QtCore.QIODevice.ReadOnly)
table_items = []
while not stream.atEnd():
# row and column where it comes from
row = stream.readInt32()
column = stream.readInt32()
map_items = stream.readInt32()
it = QtWidgets.QTableWidgetItem()

for i in range(map_items):
role = stream.readInt32()
value = QtCore.QVariant()
stream >> value
it.setData(role, value)
table_items.append(it)

for it in table_items:
print(it, it.text())


class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
list_widget = QtWidgets.QListWidget()
list_widget.setAcceptDrops(False)
list_widget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
list_widget.setDragDropMode(QtWidgets.QAbstractItemView.DragOnly)
for i in range(10):
it = QtWidgets.QListWidgetItem("item-{}".format(i))
list_widget.addItem(it)

table_widget = TableWidget(5, 10)
central_widget = QtWidgets.QWidget()
hlay = QtWidgets.QHBoxLayout(central_widget)
hlay.addWidget(list_widget)
hlay.addWidget(table_widget)
self.setCentralWidget(central_widget)


if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

关于python - pyqt5 覆盖 dropEvent python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53105235/

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