I have a simple example here. What I want to happen is when a user changes the toggle state of the 'Checkbox' i want it to set the property value for the CustomTreeWidgetItem's attr.
In this case each CustomTreeWidgetItem has an attr called dataObject which is where I store my class object Family. Family has an attribute enabled which I want this property to reflect the toggle state of the TreeWidgetItems' checkbox.
I have looked around online and have not found a solution yet for this. Most answers were in c++ or they were causing errors. You can test the results by clicking the print button. It should reflect the changes.
Here are the highlighted bits.
My custom tree widget items have the property dataObject. This is where I store my class object Family which has the property enabled in it.
self.dataObject = None
class Family:
def __init__(self, name, members=None, data=None):
self.name = name
self.enabled = True
Hope that helps.
Thanks
all the code
# 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
# 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(300, 300)
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_())
Aucun commentaire:
Enregistrer un commentaire