簡體   English   中英

來自QTreeWidgetItem內QComboBox的PyQt連接信號

[英]PyQt connecting signal from QComboBox inside the QTreeWidgetItem

我做了一個QTreeWidget,其中我有comboBox作為QTreeWidgetItem。 如何正確連接信號,以便更改的每個comboxBox索引都將更改treeWidget中的同一行?

exampleWindow

假設我將itemB行中的操作從“添加”更改為“刪除”。 它將itemB backgroundColor更改為其他內容...

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'}},
                      ]

            }

還不清楚您是如何創建每個組合框的(每次都將self.actionself.actionCombo設置為一個新的組合框嗎?)。

假設你剛剛創建一個新的QComboBox每個項目控件,最簡單的方法是只通過兩個組合框和itemwidget到信號處理程序。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM