如果已知其ItemWidget,如何获取QTreeWidgetItem

时间:2014-08-12 03:06:54

标签: python pyqt

创建LineEdit小部件后:

lineEdit=QtGui.QLineEdit()

并将其作为itemWidget分配给QTreeWidgetItem

tree=QtGui.QTreeWidget()
item=QtGui.QTreeWidgetItem(tree)
tree.setItemWidget(item, 0, lineEdit)

了解lineEdit变量如何查询作为itemWidget分配的QTreeWidgetItem?

1 个答案:

答案 0 :(得分:1)

上次编辑:2014年12月8日9:12

我的解决方案,创建自己的方法,通过部分参数QTreeWidgetItem查找QWidget,像这样(使用递归函数);

class customQTreeWidget (QtGui.QTreeWidget):
    .
    .
    .
    def findItemWidget (self, findQWidget, currentQTreeWidgetItem = None):
        if currentQTreeWidgetItem == None:
            currentQTreeWidgetItem = self.invisibleRootItem()
        for index in range(self.topLevelItemCount()):
            if findQWidget is self.itemWidget(currentQTreeWidgetItem, index):
                return currentQTreeWidgetItem
        for index in range(currentQTreeWidgetItem.childCount()):
            foundQWidget = self.findItemWidget(findQWidget, currentQTreeWidgetItem.child(index))
            if foundQWidget != None:
                return foundQWidget

然后,你想要打电话给你可以使用这个;

foundQTreeWidgetItem  = self.findItemWidget(findQWidget) # Don't part argument currentQTreeWidgetItem use in recursive loop

itemWidget方法参考http://pyqt.sourceforge.net/Docs/PyQt4/qtreewidget.html#itemWidget


此致