How to find element in QTreewidget in python?
-
Hi,
Do you mean programmatically ?
-
@MarcinOS
Well, you just parse that into separate segments byQString::split('/')
and then walk down the tree (from the top, you can do it either recursively or iteratively) matching each element against a child. What you look at depends on what you are matching each item against, e.g. perhaps it's the model'sDataRole
value. And obviously depending on what you want you can either select just the bottomelement
or all theparent
nodes leading to it as well as you go. -
@SGaist
If I may, at the risk of being shot down, this is not the right call to use here! Two reasons:-
Perform a search on all items looking at their value. Say there are a million nodes/leaves in the model! For
parent/parent/parent/parent/element
OP knows where he wants to go to, search every value and returning a list is not efficient. -
Further, for
parent/parent/parent/parent/element
searching forelement
does not tell you whether/which one found has desiredparent/parent/parent/parent
path. You are going to have to look back up ancestors of found items to discover this. Wasted effort.
Assuming OP does mean by
parent/parent/parent/parent/element
that the search is intended to pick the particular parents specified. Different if he does not actually care what those are, but that's not what the question implies (to me). -
-
Can I ask for a piece of code? I'm a very new coder, and even though I've used many PyQT elements in my application, QTreewidget is too complicated for me. I managed to fill it with data and retrieve the entire path to the selected item correctly, but searching for and selecting an item in the tree based on its path is beyond my current abilities.
-
@MarcinOS said in How to find element in QTreewidget in python?:
retrieve the entire path to the selected item correctly,
What do you mean by this? I didn't think
QTreeWidget
has an actual "path", what code did you use? Are you talking about writing your code like, say, https://stackoverflow.com/a/41042103/489865 (very first sample code box)? -
@JonB said in How to find element in QTreewidget in python?:
@SGaist
If I may, at the risk of being shot down, this is not the right call to use here! Two reasons:-
Perform a search on all items looking at their value. Say there are a million nodes/leaves in the model! For
parent/parent/parent/parent/element
OP knows where he wants to go to, search every value and returning a list is not efficient. -
Further, for
parent/parent/parent/parent/element
searching forelement
does not tell you whether/which one found has desiredparent/parent/parent/parent
path. You are going to have to look back up ancestors of found items to discover this. Wasted effort.
Assuming OP does mean by
parent/parent/parent/parent/element
that the search is intended to pick the particular parents specified. Different if he does not actually care what those are, but that's not what the question implies (to me).No reason to shoot you down. My idea was rather to use match following each level so you should only do the search on the elements from the child column of the previous item.
-
-
@SGaist said in How to find element in QTreewidget in python?:
My idea was rather to use match following each level so you should only do the search on the elements from the child column of the previous item.
I was thinking/assuming recursive. I forgot
match()
can do one level only, that changes things and makes it fine! :) -
Problem solved :)
https://stackoverflow.com/q/77850224/21149846 -