简体   繁体   English

PySide QtGui.QListWidget:如何关联到 object 数据

[英]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.

相关问题 如何从QtGui.QListWidget获取当前项目的信息? - How to get a current Item's info from QtGui.QListWidget? PySide.QtGui.QListWidget'对象没有属性'setItemSelected' - PySide.QtGui.QListWidget' object has no attribute 'setItemSelected' 改变PySide.QtGui.QListWidget与从multiprocessing.Pool异步调用发出信号导致运行时错误? - Altering PySide.QtGui.QListWidget with an emitted signal from a multiprocessing.Pool async call results in Runtime Error? AttributeError:“ PySide.QtGui.QPixmap”对象没有属性“ setDevicePixelRatio” - AttributeError: 'PySide.QtGui.QPixmap' object has no attribute 'setDevicePixelRatio' 如何在pyside中为QtGui.QFileDialog.getOpenFileName定义QDir? - How to define a QDir in pyside for QtGui.QFileDialog.getOpenFileName? 从Pyside导入QtGui时出错 - error importing QtGui from Pyside Pyside PyQt,如何将Key_Delete连接到4个不同的QListWidget - Pyside PyQt, how to connect Key_Delete to 4 different QListWidget 如何在 PyQt4/PySide 的 eventfilter 中的 QListWidget 顶部绘制? - How to paint on top of QListWidget in eventfilter in PyQt4/PySide? PySide.QtGui.QDialog问题 - PySide.QtGui.QDialog issue pyside,Qt Designer,封装的代码和AttributeError:“ MainWindow”对象没有属性“ QtGui” - pyside, Qt Designer, encapsulated code, and AttributeError: 'MainWindow' object has no attribute 'QtGui'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM