(Un)ChechkingAll from Tree View
-
Hi Everyone,
I have a problem with functions in buttons that can check or uncheck (2 seperate buttons) all Checkboxes from TreeView.
I totaly don't how to gain data from treeView to button functions.I've tried some versions with self.dlg.checkBox oraz QStrandardItem, but none of the works.
I need to do it for my project for QGIS qml add-in plugin. So that attached code is in Python
def CheckAll(self): checkBoxList =self.dlg.checkBox#or if self.dlg.checkBox.isChecked() print(checkBoxList) #to control how does looks like for checks in checkBoxList: if checkBoxList.CheckState(): checkBoxList.setCheckState(Qt.Checked) print(checkBoxList) #for checks in checkBoxList: # checks.setChecked(checked)#or child.setCheckState(Qt.Checked) def UnCheckedAll(self): checkBoxList = self.dlg.checkBox if checkBoxList.isChecked() == True: checkBoxList.setCheckState(Qt.Unchecked)#or child.setCheckState(Qt.Unchecked) print(checkBoxList)
Tree View is loaded by a different function above
for group in root: parent = QStandardItem(group.get('name')) model.appendRow(parent) parent.setCheckable(True) parent.setCheckState(Qt.Checked) parent.setUserTristate(True)#also need to develop for control in group: child = QStandardItem(control.get('name')) child.setCheckable(True) child.setCheckState(Qt.Checked) parent.appendRow([child]) self.dlg.treeView.setFirstColumnSpanned(i, self.dlg.treeView.rootIndex(), True) i = i + 1 self.dlg.treeView.show()
Thanks for every kind of help
-
Hi and welcome to devnet,
You do not need the view for that, use the model directly.
-
Can you explain what do you mean by 'model directly'?
-
Based on the fact that you are using QStandardItem, it means that you are populating a QStandardItemModel. My suggestion is that you use that QStandardItemModel directly and go through all the items you want/need to modify their value.
-
@SGaist said in (Un)ChechkingAll from Tree View:
QStandardItemModel
OK,
I put in CheckAll:x= QStandardItemModel() print(x) y= x.modelItem() print (y)
and... how to put here the checkboxes?
-
You don't put checkboxes in the model.
In your original code snippet you are correctly making the item checkable. This will trigger the view to show checkboxes. When you want to change them all, go through all the items in your model and call setCheckState on them.
-
I change modelItem to takeItem, but stil didn't catch checkboxes form treeView
-
I change modelItem to takeItem, but stil didn't catch checkboxes form treeView
@Karoluss96 said in (Un)ChechkingAll from Tree View:
I change modelItem to takeItem
Why, and what do you expect this to achieve? @SGaist said you should call
setCheckState()
on eachQStandardItem
, what about that? -
You don't put checkboxes in the model.
In your original code snippet you are correctly making the item checkable. This will trigger the view to show checkboxes. When you want to change them all, go through all the items in your model and call setCheckState on them.
Now i have in function checkall that:
checkBoxList= QStandardItem() checkBoxList.setCheckState(Qt.Checked)
no error, but when I unchecked some boxes in TreeView they don't chacked after cliking this button
-
Now i have in function checkall that:
checkBoxList= QStandardItem() checkBoxList.setCheckState(Qt.Checked)
no error, but when I unchecked some boxes in TreeView they don't chacked after cliking this button
@Karoluss96
Why would you name a singleQStandardItem()
ascheckBoxList
? Won't that just confuse you and me?You seem to be creating a
QStandardItem
into a local variable. I really don't know what you are doing with that item, if anything. You are supposed to be callingsetCheckState()
on each item in your model, which is what the view is displaying.While I notice it, you showed originally:
for checks in checkBoxList: if checkBoxList.CheckState(): checkBoxList.setCheckState(Qt.Checked) print(checkBoxList)
What is the point of a
for checks in checkBoxList
loop if you only accesscheckBoxList
inside it? -
I need an access for data in tree View, which is make in a different function.
I'll show on attached photo:
is in polish so: "zaznacz wszystko"="check all"
odznacz wszystko ="un check all".
Right, I thought that loop will be better, but all times
but I have an error: " 'QStandardItem' object is not iterable" -
I need an access for data in tree View, which is make in a different function.
I'll show on attached photo:
is in polish so: "zaznacz wszystko"="check all"
odznacz wszystko ="un check all".
Right, I thought that loop will be better, but all times
but I have an error: " 'QStandardItem' object is not iterable"@Karoluss96 said in (Un)ChechkingAll from Tree View:
but I have an error: " 'QStandardItem' object is not iterable"
Exactly, it is not. If a single
QStandardItem
, or a single checkbox, is namedcheckBoxList
it's not helpful. There is no list and nothing to loop over.I don't know what to say that @SGaist has not stated clearly in:
My suggestion is that you use that QStandardItemModel directly and go through all the items you want/need to modify their value.
calling
setCheckState(Qt.Checked)
on each item. -
yes, the variable name 'checkBoxList' is confusing, but it doesn't matter for that I need a loop for all Checkboxes as I shown in the print screen above
-
yes, the variable name 'checkBoxList' is confusing, but it doesn't matter for that I need a loop for all Checkboxes as I shown in the print screen above
@Karoluss96 said in (Un)ChechkingAll from Tree View:
yes, the variable name 'checkBoxList' is confusing,
Then change it, and get rid of " " 'QStandardItem' object is not iterable"" error.
I need a loop for all Checkboxes as I shown in the print screen above
And I referred you to @SGaist's answer to that, which is what you need to do.
-
@JonB said in (Un)ChechkingAll from Tree View:
@SGaist can you explain more what do you mean?
-
@JonB said in (Un)ChechkingAll from Tree View:
@SGaist can you explain more what do you mean?
@SGaist said in (Un)ChechkingAll from Tree View:
When you want to change them all, go through all the items in your model and call setCheckState on them.
-
Do you know how to go through all the items/rows in your model? Suppose you wanted to print out (or change) the text of each item, like
Kontrola atrybutowe
,Kontrola 1
,Kontrola 2
, etc. How would you do that? -
Once you have the loop for 1, presumably you know how to call
setCheckState(Qt.Checked)
on each item visited.
-
-
- If put print (QStandardItemModel()) I gain: <PyQt5.QtGui.QStandardItemModel object at 0x00000229EA8F7160>
all data in Tree View come from parsed xml (to variable tree) file (I can't publish them), by
root = tree.getroot()
and later
for grupaKontroli in root: parent = QStandardItem(grupaKontroli.get('name')) QStandardItemModel.appendRow(parent) parent.setCheckable(True) setCheckState(Qt.Checked)
- Answer for that is in function above
This function in the end of main post
- If put print (QStandardItemModel()) I gain: <PyQt5.QtGui.QStandardItemModel object at 0x00000229EA8F7160>
-
- If put print (QStandardItemModel()) I gain: <PyQt5.QtGui.QStandardItemModel object at 0x00000229EA8F7160>
all data in Tree View come from parsed xml (to variable tree) file (I can't publish them), by
root = tree.getroot()
and later
for grupaKontroli in root: parent = QStandardItem(grupaKontroli.get('name')) QStandardItemModel.appendRow(parent) parent.setCheckable(True) setCheckState(Qt.Checked)
- Answer for that is in function above
This function in the end of main post
@Karoluss96
No. I'm afraid you do not seem to have a grasp of models/what is going on. You might benefit from reading up on this in the documentation.The answer to, for example, how do I set every checkbox in my model would be:
for i in range(model.rowCount()): model.item(i).setCheckState(Qt.Checked)
If you want to only check items in and below some one particular node you would need to recurse down its descendants. But you asked for check every item.
- If put print (QStandardItemModel()) I gain: <PyQt5.QtGui.QStandardItemModel object at 0x00000229EA8F7160>
-
Looks useful, pretty and short, but I still didn't see its results- It need to take data from treeView!
-
Looks useful, pretty and short, but I still didn't see its results- It need to take data from treeView!
@Karoluss96 said in (Un)ChechkingAll from Tree View:
It need to take data from treeView!
A
QTreeView
does not store any data. It's a view onto a model. The model is where you store the data. @SGaist has said this, I have said this. I suggested you might like to read up on Qt model/view architecture.