Get FreeCAD object associated with QTreeWidgetItem

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
paulo_cmv
Posts: 15
Joined: Tue Oct 20, 2020 7:43 pm

Get FreeCAD object associated with QTreeWidgetItem

Post by paulo_cmv »

Im working in one macro and i need to walk the treview and manipulate their objects (Part, Body, groups, etc).
I cant find how to get the object for a given treeview item. Text, data(0,0) and data(0,2) appears to be the label but label is not unique.
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Get FreeCAD object associated with QTreeWidgetItem

Post by openBrain »

As strange as it can be, FreeCAD enforces uniqueness of labels too.
paulo_cmv
Posts: 15
Joined: Tue Oct 20, 2020 7:43 pm

Re: Get FreeCAD object associated with QTreeWidgetItem

Post by paulo_cmv »

So theres no "more correct" way?
Must i use ActiveDocument.getObjectsByLabel()[0]?
Theres no way to get internal name from treeview?
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Get FreeCAD object associated with QTreeWidgetItem

Post by ickby »

openBrain wrote: Tue Mar 30, 2021 5:41 pm As strange as it can be, FreeCAD enforces uniqueness of labels too.
That is not always true. The user can disable the enforcing of uniqueness in the Preferences.
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Get FreeCAD object associated with QTreeWidgetItem

Post by openBrain »

ickby wrote: Tue Mar 30, 2021 6:12 pm That is not always true. The user can disable the enforcing of uniqueness in the Preferences.
My mistake. Thanks for pointing out my ignorance. ;)
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Get FreeCAD object associated with QTreeWidgetItem

Post by openBrain »

paulo_cmv wrote: Tue Mar 30, 2021 6:07 pm So theres no "more correct" way?
Must i use ActiveDocument.getObjectsByLabel()[0]?
Theres no way to get internal name from treeview?
I found back this macro I already wrote : https://forum.freecadweb.org/viewtopic. ... et#p446393
I didn't test again, but .text(0) seems to hold the name.
paulo_cmv
Posts: 15
Joined: Tue Oct 20, 2020 7:43 pm

Re: Get FreeCAD object associated with QTreeWidgetItem

Post by paulo_cmv »

openBrain wrote: Tue Mar 30, 2021 7:40 pm I found back this macro I already wrote : https://forum.freecadweb.org/viewtopic. ... et#p446393
I didn't test again, but .text(0) seems to hold the name.
The macro is looking for label

openBrain said just before code, that "That's all my mistake. :) I didn't pay attention that the text in the tree is the Label, while the function is searching for the Name.
Here is the fix :"

Code: Select all

 
     def recFindChild(docitem, objname):
        for i in range(0, docitem.childCount()):
            if docitem.child(i).text(0) == objname: <-- objname is object.Label passed as argument
            ...
 
    obj = object.Label
    ...
            objitem = recFindChild(docitem, obj)
 
Post Reply