我制作了一个QTreeWidget,其中comboBox作为QTreeWidgetItem。如何正确连接信号,使每个comboxBox索引更改后都会更改树窗口中的同一行?在
假设我将行itemB中的操作从Add更改为Remove。它会将itemB的背景颜色改成。。。在
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数据构造字典?这样我就可以得到用户为每个项目的操作选择了什么,并以字典的形式报告如下?在
^{pr2}$
目前还不清楚您是如何创建每个组合框的(是否每次都将}设置为新的组合框?)。在
self.action
和{假设您只是为每个item小部件创建一个新的
QComboBox
,最简单的方法是将组合框和itemwidget都传递给信号处理程序。在相关问题 更多 >
编程相关推荐