PyQt连接来自QTreeWidgetItem内的QComboBox的信号

时间:2016-06-10 18:10:31

标签: python pyqt qcombobox qtreewidget qtreewidgetitem

我已经创建了一个QTreeWidget,其中我将comboBox作为QTreeWidgetItem。如何正确连接信号,以便更改每个comboxBox索引将更改treeWidget中的同一行?

exampleWindow

假设我将行项目B中的操作从“添加”更改为“删除”。它会将itemB backgroundColor更改为somethingelse ...

data = { 'GroupA': [
                  {'itemA': {'action' : 'Add'}},
                  {'itemB':{'action' : 'Updates'}},
                  ],
        'GroupB': [
                  {'someOtherItemA': {'action' : 'Updates'}},
                  {'someOtherItemA':{'action' : 'Add'}},
                  ]

        }


class Window(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)

        self.treeWidget=QtGui.QTreeWidget(self)
        self.treeWidget.setGeometry(QtCore.QRect(50,20,450,240))

        self.header=QtGui.QTreeWidgetItem(["Item","Action"])
        self.treeWidget.setHeaderItem(self.header)

        for group in data:
            groupItem=QtGui.QTreeWidgetItem(self.treeWidget)
            groupItem.setText(0,group)
            groupItem.setFlags( QtCore.Qt.ItemIsEnabled )

            for itemDict in data[group]:
                for item in itemDict:

                    itemWidget=QtGui.QTreeWidgetItem(groupItem, [item])
                    itemWidget.setText(0, item)

                    action = itemDict[item]['action']
                    self.action = self._actionCombo()
                    self.treeWidget.setItemWidget(itemWidget, 1, self.actionCombo)
                    slotLambda = lambda: self.actionChanged(itemWidget)
                    self.action.currentIndexChanged.connect(slotLambda)

        self.treeWidget.expandAll()


    @QtCore.pyqtSlot(QtGui.QTreeWidgetItem)
    def actionChanged(self, treeWidgetItem):
        treeWidgetItem.setBackgroundColor(0, QtGui.QColor(0,0,0))

    def _actionCombo(self):

        self.actionCombo = QtGui.QComboBox()
        actionLists = ['Add', 'Updates', 'Keep', 'Remove']
        for actionItem in actionLists:
            self.actionCombo.addItem(actionItem)

        return self.actionCombo

    def report(self):
        #construct the data back in a dictionary

        newData = {}
        return newData

另一个问题是我如何根据QtreeWidget数据构建字典?因此,我将获得用户为每个项目选择的操作,并将其作为字典报告回来,如下所示?

dataReportBack = { 'GroupA': [
                      {'itemA': {'action' : 'Add'}},
                      {'itemB':{'action' : 'Updates'}},
                      ],
            'GroupB': [
                      {'someOtherItemA': {'action' : 'Updates'}},
                      {'someOtherItemA':{'action' : 'Add'}},
                      ]

            }

1 个答案:

答案 0 :(得分:1)

您是不是很清楚如何创建每个组合框(每次都将self.actionself.actionCombo设置为新的组合框?)。

假设您只是为每个项目小部件创建一个新的QComboBox,最简单的方法就是将两者组合框和itemwidget传递给信号处理程序。 / p>

class Widget(...)
    ...

    def func(self):        
        ...
        for item in itemDict:
            itemWidget = QtGui.QTreeWidgetItem(groupItem, [item])
            itemWidget.setText(0, item)
            # I'm guessing this creates a new QComboBox
            actionCombo = self._actionCombo()
            self.treeWidget.setItemWidget(itemWidget, 1, actionCombo)
            actionCombo.currentIndexChanged.connect(lambda: self.on_actionComboChanged(actionCombo, itemWidget)

    def on_actionComboChanged(self, actionCombo, itemWidget)
        if actionCombo.currentText() == 'Add':
            color = QtGui.QColor(QtCore.Qt.green)
        else:
            color = QtGui.QColor(QtCore.Qt.red)
        itemWidget.setData(QtCore.Qt.BackgroundRole, QtGui.QBrush(color))