gpt4 book ai didi

python - 移动项目时,QStandardItem 无法正确克隆

转载 作者:行者123 更新时间:2023-12-01 00:24:30 25 4
gpt4 key购买 nike

如以下代码所示,当您拖放一个项目(使用 clone() 方法从 QStandardItem 子类化)时,您将获得一个 QStandardItem 而不是子类。此外,存储在类中或作为 setData 一部分的数据会丢失。我怀疑这是因为无法“序列化”数据。但我不知道如何“保存”数据或元。如何保留QObject?下面的代码工作正常,但是一旦移动分支节点,分支和分支中的所有节点都会变成 QStandardItem 的而不是 myItem 并丢失数据(如果他们有任何)。

# -*- coding: utf-8 -*-
"""
Created on Mon Nov 4 09:10:16 2019

Test of Tree view with subclassed QStandardItem and Drag and Drop
enabled. When you move a parent the parent looses the subclass and thus
the meta - however, it also looses the data: This is likely because
the data cannot be serialized. How to fix?

@author: tcarnaha
"""
import sys
from PyQt5 import QtGui, QtWidgets, QtCore


class myData():
def __init__(self, title):
self._title = title
self._stuff = dict()
self._obj = QtCore.QObject()

@property
def obj(self):
return self._obj

@obj.setter
def obj(self, value):
self._obj = value

@property
def title(self):
return self._title

@title.setter
def title(self, value):
self._title = value


class myItem(QtGui.QStandardItem):
def __init__(self, parent=None):
super(myItem, self).__init__(parent)
self._meta = None

@property
def meta(self):
return self._meta

@meta.setter
def meta(self, value):
self._meta = value

def clone(self):
print "My cloning"
old_data = self.data()
print "Old data [{}]".format(old_data)
old_meta = self.meta
obj = myItem()
obj.setData(old_data)
print "New data [{}]".format(obj.data())
obj.meta = old_meta
print "Clone is a ", obj.__class__
return obj

class mainWidget(QtWidgets.QMainWindow):
def __init__(self):
super(mainWidget, self).__init__()
self.model = QtGui.QStandardItemModel()
self.model.setItemPrototype(myItem())
self.view = QtWidgets.QTreeView()
self.view.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.view.customContextMenuRequested.connect(self.list_click)
self.view.setDragDropMode(QtWidgets.QAbstractItemView.InternalMove)
self.view.setDefaultDropAction(QtCore.Qt.MoveAction)
self.view.setDragDropOverwriteMode(False)
self.view.setAcceptDrops(True)
self.view.setDropIndicatorShown(True)
self.view.setDragEnabled(True)
self.view.setModel(self.model)
dataA = myData('A thing')
parentA = myItem()
parentA.setText('A')
parentA.setDragEnabled(True)
parentA.setDropEnabled(True)
parentA.setData(dataA)
parentA.meta = QtCore.QObject()
childa = myItem()
childa.setText('a')
childb = myItem()
childb.setText('b')
childc = myItem()
childc.setText('c')
parentA.appendRows([childa, childb, childc])
dataB = myData('B thing')
parentB = myItem()
parentB.setText('B')
parentB.setDragEnabled(True)
parentB.setDropEnabled(True)
parentB.setData(dataB)
parentB.meta = QtCore.QObject()
childd = myItem()
childd.setText('d')
childe = myItem()
childe.setText('e')
childf = myItem()
childf.setText('f')
parentB.appendRows([childd, childe, childf])
self.model.appendRow(parentA)
self.model.appendRow(parentB)

classAct = QtWidgets.QAction('Class', self)
classAct.triggered.connect(self.classIs)
dataAct = QtWidgets.QAction('Data', self)
dataAct.triggered.connect(self.dataIs)
metaAct = QtWidgets.QAction('Meta', self)
metaAct.triggered.connect(self.metaIs)
self.menu = QtWidgets.QMenu("Item info")
self.menu.addAction(classAct)
self.menu.addAction(dataAct)
self.menu.addAction(metaAct)

self.setCentralWidget(self.view)

@QtCore.pyqtSlot(QtCore.QPoint)
def list_click(self, position):
self.menu.popup(self.view.viewport().mapToGlobal(position))

def classIs(self):
selected_indexes = self.view.selectedIndexes()
for index in selected_indexes:
item = self.model.itemFromIndex(index)
print "Item {} Class {} ".format(item.text(), item.__class__())

def dataIs(self):
selected_indexes = self.view.selectedIndexes()
for index in selected_indexes:
item = self.model.itemFromIndex(index)
try:
print "Item {} data {} Object {}".format(item.text(),
item.data().title,
item.data().obj)
except Exception as exc:
print "Data exception ", exc

def metaIs(self):
selected_indexes = self.view.selectedIndexes()
for index in selected_indexes:
item = self.model.itemFromIndex(index)
try:
print "Item {} meta {} ".format(item.text(), item.meta)
except Exception as exc:
print "Meta exception ", exc


if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main = mainWidget()
main.show()
app.exec_()

最佳答案

这里有几个与 Qt 和 PyQt 如何序列化对象相关的问题。首先,当克隆 QStandardItem 时,仅复制标志和数据 - 其他所有内容都将被忽略(包括动态 python 属性)。其次,没有办法直接复制QObject。这是因为它无法转换为 QVariant(Qt 用于序列化)并且无法进行 pickle(PyQt 用于序列化)。

为了解决第二个问题,我们需要保留对所有QObject实例的单独引用,然后使用间接键稍后再次访问它们。可能有很多不同的方法来实现这一点,但这里有一个非常简单的方法来说明基本思想:

objects = {}

class MyObject(QtCore.QObject):
def __init__(self, parent=None):
super(MyObject, self).__init__(parent)
self.setProperty('key', max(objects.keys() or [0]) + 1)
objects[self.property('key')] = self

因此,这会自动将每个实例添加到全局缓存中,并为其提供唯一的查找键,以便以后可以轻松找到它。完成此操作后,现在需要调整 myData 类以使用 MyObject 类,以便 pickling is handled correctly :

class myData():
def __init__(self, title):
self._title = title
self._stuff = dict()
self._obj = MyObject()

def __setstate__(self, state):
self._obj = objects.get(state['obj'])
self._stuff = state['stuff']
self._title = state['title']

def __getstate__(self):
return {
'obj': self._obj and self._obj.property('key'),
'title': self._title,
'stuff': self._stuff,
}

解决第一个问题要简单得多:我们只需要确保任何动态 python 属性使用自定义数据角色将其基础值存储在项目的数据中。在这种特殊情况下,该值必须是项目的 MyObject 实例的键,以便在拖放操作后可以检索它:

class myItem(QtGui.QStandardItem):
MetaRole = QtCore.Qt.UserRole + 1000

@property
def meta(self):
return objects.get(self.data(myItem.MetaRole))

@meta.setter
def meta(self, value):
self.setData(value.property('key'), myItem.MetaRole)

def clone(self):
print "My cloning"
obj = myItem(self)
print "Clone is a ", obj.__class__
return obj

下面是原始脚本的工作版本,它实现了上述所有内容。但请记住,您几乎肯定需要对此进行调整,以便与您的真实代码正常工作。这只是一个有效的概念验证,展示了如何处理上述两个问题。

# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtGui, QtWidgets, QtCore

objects = {}

class MyObject(QtCore.QObject):
def __init__(self, parent=None):
super(MyObject, self).__init__(parent)
self.setProperty('key', max(objects.keys() or [0]) + 1)
objects[self.property('key')] = self

class myData():
def __init__(self, title):
self._title = title
self._stuff = dict()
self._obj = MyObject()

def __setstate__(self, state):
self._obj = objects.get(state['obj'])
self._stuff = state['stuff']
self._title = state['title']

def __getstate__(self):
return {
'obj': self._obj.property('key'),
'title': self._title,
'stuff': self._stuff,
}

@property
def obj(self):
return self._obj

@obj.setter
def obj(self, value):
self._obj = value

@property
def title(self):
return self._title

@title.setter
def title(self, value):
self._title = value

class myItem(QtGui.QStandardItem):
MetaRole = QtCore.Qt.UserRole + 1000

@property
def meta(self):
return objects.get(self.data(myItem.MetaRole))

@meta.setter
def meta(self, value):
self.setData(value.property('key'), myItem.MetaRole)

def clone(self):
print "My cloning"
obj = myItem(self)
print "Clone is a ", obj.__class__
return obj

class mainWidget(QtWidgets.QMainWindow):
def __init__(self):
super(mainWidget, self).__init__()
self.model = QtGui.QStandardItemModel()
self.model.setItemPrototype(myItem())
self.view = QtWidgets.QTreeView()
self.view.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.view.customContextMenuRequested.connect(self.list_click)
self.view.setDragDropMode(QtWidgets.QAbstractItemView.InternalMove)
self.view.setDefaultDropAction(QtCore.Qt.MoveAction)
self.view.setDragDropOverwriteMode(False)
self.view.setAcceptDrops(True)
self.view.setDropIndicatorShown(True)
self.view.setDragEnabled(True)
self.view.setModel(self.model)
dataA = myData('A thing')
parentA = myItem()
parentA.setText('A')
parentA.setDragEnabled(True)
parentA.setDropEnabled(True)
parentA.setData(dataA)
parentA.meta = MyObject()
childa = myItem()
childa.setText('a')
childb = myItem()
childb.setText('b')
childc = myItem()
childc.setText('c')
parentA.appendRows([childa, childb, childc])
dataB = myData('B thing')
parentB = myItem()
parentB.setText('B')
parentB.setDragEnabled(True)
parentB.setDropEnabled(True)
parentB.setData(dataB)
parentB.meta = MyObject()
childd = myItem()
childd.setText('d')
childe = myItem()
childe.setText('e')
childf = myItem()
childf.setText('f')
parentB.appendRows([childd, childe, childf])
self.model.appendRow(parentA)
self.model.appendRow(parentB)

classAct = QtWidgets.QAction('Class', self)
classAct.triggered.connect(self.classIs)
dataAct = QtWidgets.QAction('Data', self)
dataAct.triggered.connect(self.dataIs)
metaAct = QtWidgets.QAction('Meta', self)
metaAct.triggered.connect(self.metaIs)
self.menu = QtWidgets.QMenu("Item info")
self.menu.addAction(classAct)
self.menu.addAction(dataAct)
self.menu.addAction(metaAct)

self.setCentralWidget(self.view)

@QtCore.pyqtSlot(QtCore.QPoint)
def list_click(self, position):
self.menu.popup(self.view.viewport().mapToGlobal(position))

def classIs(self):
selected_indexes = self.view.selectedIndexes()
for index in selected_indexes:
item = self.model.itemFromIndex(index)
print "Item {} Class {} ".format(item.text(), item.__class__())

def dataIs(self):
selected_indexes = self.view.selectedIndexes()
for index in selected_indexes:
item = self.model.itemFromIndex(index)
try:
print "Item {} data {} Object {}".format(item.text(),
item.data().title,
item.data().obj)
except Exception as exc:
print "Data exception ", exc

def metaIs(self):
selected_indexes = self.view.selectedIndexes()
for index in selected_indexes:
item = self.model.itemFromIndex(index)
try:
print "Item {} meta {} ".format(item.text(), item.meta)
except Exception as exc:
print "Meta exception ", exc


if __name__ == '__main__':

app = QtWidgets.QApplication(sys.argv)
main = mainWidget()
main.show()
app.exec_()

关于python - 移动项目时,QStandardItem 无法正确克隆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58698676/

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