如何在QTreeWidget第二列QComboBox中访问内容?

时间:2018-11-12 16:56:22

标签: python pyqt qcombobox qtreewidget

我有一个AppDialog(QtGui.QWidget),其中包含一个QtreeWidget

self.tree = QtGui.QTreeWidget()

我使用函数self.sequences将数据从seqTree填充到此QtreeWidget。

来自self.sequences的数据:

{'090': 
  {'090-0010': [
    {'code': '090-0010_v000', 'type': 'Version', 'id': 26676, 'entity.Shot.sg_sequence.Sequence.code': '090', 'entity.Shot.code': '090-0010'}, 
    {'code': '090-0010_maquette_v001', 'type': 'Version', 'id': 27848, 'entity.Shot.sg_sequence.Sequence.code': '090', 'entity.Shot.code': '090-0010'}, 
    {'code': '090-0010_maquette_v002', 'type': 'Version', 'id': 27849, 'entity.Shot.sg_sequence.Sequence.code': '090', 'entity.Shot.code': '090-0010'}]}, 
'082': 
  {'082-0020': [
    {'code': '082-0020_compo_v000', 'type': 'Version', 'id': 28748, 'entity.Shot.sg_sequence.Sequence.code': '082', 'entity.Shot.code': '082-0020'}}

功能seqTree

def seqTree(self):  
    for seq in sorted(self.sequences):
        if self.sequences[seq]:
            parent = QtGui.QTreeWidgetItem(self.tree)
            parent.setFlags(parent.flags() | QtCore.Qt.ItemIsTristate | QtCore.Qt.ItemIsUserCheckable)
            parent.setCheckState(0, QtCore.Qt.Unchecked)
            parent.setText(0,unicode(seq))
            for shot in self.sequences[seq]:
                child = QtGui.QTreeWidgetItem(parent)
                child.setFlags(child.flags() | QtCore.Qt.ItemIsUserCheckable)
                child.setCheckState(0, QtCore.Qt.Unchecked)
                child.setText(0,unicode(shot))
                versionCombo = QtGui.QComboBox(self.tree)
                versionCombo.setFixedWidth(200)
                for version in sorted(self.sequences[seq][shot], reverse=True):
                    versionCombo.addItem(version['code'])
                self.tree.setItemWidget(child, 1, versionCombo)

此解决方案适用于填充数据。我的QTreeWidget由第一列的结构,然后是第一列中的第二个子键构成。在第二列中,我添加了一个QComboBox及其代码中的内容,例如'090-0010_maquette_v001'。

但是,我无法检索ComboBox的内容。 要检索选择,我使用以下功能:

def checkedList(self):
    checked = dict()
    self.selection = []
    root = self.tree.invisibleRootItem()
    signal_count = root.childCount()

    for i in range(signal_count):
        signal = root.child(i)
        checked_sweeps = list()
        num_children = signal.childCount()

        for n in range(num_children):
            child = signal.child(n)
            if child.checkState(0) == QtCore.Qt.Checked:
                checked_sweeps.append(child.text(0))
                #### I need here to retrieve the content of ComboBox.

        checked[signal.text(0)] = checked_sweeps

    for item in sorted(checked):
        if not checked[item] == []:
            for shot in checked[item]: 
                self.selection.append(shot)

我真的找不到如何到达child的第二列。我已经尝试过多种操作,例如child.data()indexFromItem()。我是否需要将ComboBox链接到该类才能找到它?还是到QtreeWidget的链接就足够了?

1 个答案:

答案 0 :(得分:1)

您必须使用QComboBox来获得itemWidget

if child.checkState(0) == QtCore.Qt.Checked:
    checked_sweeps.append(child.text(0))
    combo = self.tree.itemWidget(child, 1)
    print(combo.currentText())