[英]PySide QtGui.QListWidget: how to associate to object data
I am building a GUI in FreeCAD using the built-in PySide module.我正在使用内置的 PySide 模块在 FreeCAD 中构建一个 GUI。 Is there a simple/standard way to 'attach' data to a list item in a PySide QtGui.QListWidget?
是否有一种简单/标准的方法可以将数据“附加”到 PySide QtGui.QListWidget 中的列表项? So far, I have this (note this is not the complete code, but hopefully gets the point across):
到目前为止,我有这个(注意这不是完整的代码,但希望能得到理解):
Class fcGui(QtGui.QWidget):
def initUI(self):
self.listWidget = QtGui.QListWidget(self)
self.objData = []
self.listWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
for i in range(0,len(App.ActiveDocument.Objects)):
self.listWidget.addItem(App.ActiveDocument.Objects[i].Name)
def acceptance(self):
print(self.listWidget.selectedItems())
This prints the selected items by name.这将按名称打印所选项目。 But now I need to do something with the objects associated with these names.
但现在我需要对与这些名称关联的对象做一些事情。 Is there a straightforward way to do this?
有没有一种简单的方法可以做到这一点? All I can come up with is a complicated scheme to determine the index of each selected item, and associate that with a list of the corresponding objects.
我所能想出的只是一个复杂的方案来确定每个选定项目的索引,并将其与相应对象的列表相关联。 Any help would be appreciated.
任何帮助,将不胜感激。
You can use a custom role to store the object:您可以使用自定义角色来存储 object:
ObjectRole = QtCore.Qt.UserRole + 1000
Class fcGui(QtGui.QWidget):
def initUI(self):
self.listWidget = QtGui.QListWidget(self)
self.objData = []
self.listWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
for obj in App.ActiveDocument.Objects:
it = QtGui.QListWidgetItem(obj.Name)
it.setData(ObjectRole, obj)
self.listWidget.addItem(it)
def acceptance(self):
for it in self.listWidget.selectedItems():
obj = it.data(ObjectRole)
print(obj, obj.Name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.