我在这里有一个简单的例子。我想要发生的是当用户更改“复选框”的切换状态时。我希望它为CustomTreeWidgetItem的attr。
设置属性值在这种情况下,每个CustomTreeWidgetItem都有一个名为dataObject的attr,它是我存储类对象Family的地方。系列具有启用属性,我希望此属性能够反映TreeWidgetItems的切换状态。复选框。
我在网上看了一下,还没有找到解决方案。大多数答案都是用c ++编写的,否则会导致错误。您可以通过单击打印按钮来测试结果。它应该反映出这些变化。
以下是突出显示的位。
更新 链接的问题没有回答我的问题。它不会返回单击的复选框的值。
这段代码用简单的术语表示什么?我需要修改什么才能将数据对象设置为True或False?目前它返回0或2.我不确定这是三态还是什么。希望有人能解释发生了什么。
if column == 0 and role == 10:
我的自定义树窗口小部件项目具有 dataObject 属性。这是我存储我的类对象 Family 的地方,其中包含启用属性。
self.dataObject = None
class Family:
def __init__(self, name, members=None, data=None):
self.name = name
self.enabled = True
希望有所帮助。
由于
所有代码
# Imports
# ------------------------------------------------------------------------------
import sys
from PySide import QtGui, QtCore
# Class
# ------------------------------------------------------------------------------
class Family:
def __init__(self, name, members=None, data=None):
self.name = name
self.enabled = True
# Custom Tree Widget
# ------------------------------------------------------------------------------
class TreeNodeItem( QtGui.QTreeWidgetItem ):
def __init__( self, parent, name ):
## Init super class ( QtGui.QTreeWidgetItem )
super( TreeNodeItem, self ).__init__( parent )
# Column 0 - Text:
self.setText( 0, name )
# Tree Node Data
self.dataObject = None
def setData(self, column, role, value):
if column == 0 and role == 10:
self.dataObject.enabled = value
super(TreeNodeItem, self).setData(column, role, value)
# Variables
# ------------------------------------------------------------------------------
Families = [
Family("Smith"),
Family("Michaels"),
Family("Wislon"),
Family("Yoder")
]
# Main
# ------------------------------------------------------------------------------
class Example(QtGui.QWidget):
def __init__(self,):
super(Example, self).__init__()
self.initUI()
def initUI(self):
# formatting
self.resize(400, 400)
self.setWindowTitle("Example")
# widgets
self.printData = QtGui.QPushButton("Print Families Data")
self.itemList = QtGui.QTreeWidget()
self.itemList.setItemsExpandable(True)
self.itemList.setAnimated(True)
self.itemList.setItemsExpandable(True)
self.itemList.setColumnCount(1)
self.itemList.setHeaderLabels(['Families'])
# signals
self.printData.clicked.connect(self.PrintAllData)
# layout - row/column/verticalpan/horizontalspan
self.mainLayout = QtGui.QGridLayout(self)
self.mainLayout.addWidget(self.itemList,0,0)
self.mainLayout.addWidget(self.printData,1,0)
self.center()
self.show()
self.UpdateFamilies()
def center(self):
qr = self.frameGeometry()
cp = QtGui.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def PrintAllData(self):
for f in Families:
print f.name, f.enabled
def UpdateFamilies(self):
treeWidget = self.itemList
treeWidget.clear()
for f in Families:
# Add Families
treeNode = TreeNodeItem(treeWidget, f.name)
# assign family class object to dataObject attr of treenode
treeNode.dataObject = f
treeNode.setCheckState(0, QtCore.Qt.Checked)
# Main
# ------------------------------------------------------------------------------
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
答案 0 :(得分:1)
答案在链接问题中(尽管在C ++中)Is it possible to create a signal for when a QTreeWidgetItem checkbox is toggled?
简短回答 - 不,这似乎不是该复选框的信号。有两种方法可以解决这个问题:
1)与链接问题一样,您可以实现SetData方法并同时更新数据:
# Custom Tree Widget
# ------------------------------------------------------------------------------
class TreeNodeItem( QtGui.QTreeWidgetItem ):
def __init__( self, parent, name ):
## Init super class ( QtGui.QTreeWidgetItem )
super( TreeNodeItem, self ).__init__( parent )
# Column 0 - Text:
self.setText( 0, name )
# Tree Node Data
self.dataObject = None
def setData(self, column, role, value):
if column == 0 and role == 10:
self.dataObject.enabled = value
super(TreeNodeItem, self).setData(column, role, value)
2)您也可以尝试手动创建一个复选框,连接它的信号并使用QTreeWidget.setItemWidgem
将其添加到TreeNodeItem