从Qtable获取内容,这是QTreeWidget的孩子

时间:2017-10-08 13:33:44

标签: python pyqt4 qtreewidgetitem

我创建了一个包含表格的子窗口小部件。我想访问QtableWidget的内容,但我找不到怎么做?

treewidget看起来像: treewidget with tables as children

我已经生成了树形图,如:

        software = QTreeWidgetItem(['Software'])
        hardware = QTreeWidgetItem(['Hardware'])
        beide = QTreeWidgetItem(['Beide'])
        andere = QTreeWidgetItem(['Andere'])          
        i = 0
        for key, value in sorted(data.items()):
                if value['Subtype'] == 'Software':
                    sub = software
                if value['Subtype'] == 'Hardware':
                    sub = hardware
                if value['Subtype'] == 'Beide':
                    sub = beide
                if value['Subtype'] == 'Andere':
                    sub = andere

                l1 = QTreeWidgetItem(sub)

                if value['Privacy'] == 'Voorzichtig':
                    l1.setBackgroundColor(0, QColor('orange'))
                if value['Privacy'] == 'Vertrouwelijk':
                    l1.setBackgroundColor(0, QColor('red'))
                    l1.setTextColor(0, QColor('white'))

                l1.setText(0, value['sDesc'])

                self.treeMainDisplay.addTopLevelItem(l1)

                l1_child = QTreeWidgetItem(l1)

                self.item_table = QTableWidget()
                self.item_table.verticalHeader().setVisible(False)
                self.item_table.horizontalHeader().setVisible(False)
                self.item_table.setColumnCount(5)
                self.item_table.setRowCount(5)
                c1_item = QTableWidgetItem("%s" % value['sDesc'].encode('utf-8'))
                self.item_table.setItem(0, 0, c1_item)
                c2_item = QTableWidgetItem("%s" % value['Type'].encode('utf-8'))
                self.item_table.setItem(1,0, c2_item)

                qt_child = self.treeMainDisplay.setItemWidget(l1_child, 0,  self.item_table)  

        self.treeMainDisplay.addTopLevelItem(software)
        self.treeMainDisplay.addTopLevelItem(hardware)
        self.treeMainDisplay.addTopLevelItem(beide)
        self.treeMainDisplay.addTopLevelItem(andere)

我在遍历treewidgetitems,但不知道如何访问表格内容:

def testItems(self):
    iterator = QTreeWidgetItemIterator(self.treeMainDisplay)
    while iterator.value():
        item = iterator.value()
        if not item.text(0):
            #Get Table Object?
            # item.item(0,0).text()
        else:
            print item.text(0)
        iterator += 1

似乎我无法访问QTableWidget对象,我只获得了QTreeWidgetItem对象。

所有反馈都非常感谢!

1 个答案:

答案 0 :(得分:1)

必须使用itemWidget方法通过树窗口小部件访问项目窗口小部件:

def testItems(self):
    iterator = QTreeWidgetItemIterator(self.treeMainDisplay)
    while iterator.value():
        item = iterator.value()
        if not item.text(0):
            # Get Table Object
            table = self.treeMainDisplay.itemWidget(item, 0)
        else:
            print item.text(0)
        iterator += 1