2016-06-10 121 views
0

我做了一個QTreeWidget,我有comboBox作爲QTreeWidgetItem。如何正確連接信號,以便每個comboxBox索引都會更改treeWidget中的同一行?PyQt連接信號從QComboBox裏面的QTreeWidgetItem

exampleWindow

說,如果我改變從添加行itemB行動刪除。它會改變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

它,你是如何創建每個組合框的真不明白(是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))