How to reach content in QTreeWidget second colum QComboBox?

喜你入骨 提交于 2020-02-15 10:17:21

问题


I have an AppDialog(QtGui.QWidget) containing a QtreeWidget.

self.tree = QtGui.QTreeWidget()

I populate data from self.sequences to this QtreeWidget with the function seqTree.

Datas from 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'}}

Function 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)

This solution works to populate datas. My QTreeWidget is structure by first keys, then as child, second keys in the first column. In the second column, I add a QComboBox with the content from code, for exemple '090-0010_maquette_v001'.

But then, I'm unable to retrieve content of the ComboBox. To retrieve the selection, I use the following function:

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)

I really can't find how to reach the second column of child. I've tried multiple things like child.data(), indexFromItem(). Do I need to link the ComboBox to the class to be able to find it? or the link to the QtreeWidget is enough?


回答1:


You have to get the QComboBox with itemWidget:

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


来源:https://stackoverflow.com/questions/53266775/how-to-reach-content-in-qtreewidget-second-colum-qcombobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!