Tree update issue?: tree.selectedItems()

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Tree update issue?: tree.selectedItems()

Post by Roy_043 »

I am trying to select the children of a Draft Layer in the tree.

Std_TreeSyncSelection is switched off.

If I test the code by pasting each line in the Python console everything works fine. But running the whole code results in an error:
Traceback (most recent call last):
File "C:/Downloads/test2.py", line 23, in <module>
item = tree.selectedItems()[0]
<class 'IndexError'>: list index out of range


It is this code sequence that causes the problem:

Code: Select all

Gui.Selection.addSelection(grp) # Need to update the tree after this? But how?
item = tree.selectedItems()[0]
Gui.Selection.clearSelection()

Test file attached.

The whole code:

Code: Select all

# https://wiki.freecadweb.org/Macro_ExpandTreeItem

from PySide import QtGui

def expandParents(tree, item):
    parent = item.parent()
    if parent:
        tree.expandItem(parent)
        expandParents(tree, parent)

doc = App.ActiveDocument

mw = Gui.getMainWindow()
trees = mw.findChildren(QtGui.QTreeWidget)
tree = trees[0]

obj = App.ActiveDocument.Line003
grp = obj.InList[0]

Gui.Selection.clearSelection()

Gui.Selection.addSelection(grp)
item = tree.selectedItems()[0]
Gui.Selection.clearSelection()

expandParents(tree, item)

Gui.Selection.addSelection(grp)
item = tree.selectedItems()[0]
Gui.Selection.clearSelection()

tree.expandItem(item)

for i in range(item.childCount()):
    name = item.child(i).setSelected(True)
Attachments
groups_and_layers.FCStd
(17.33 KiB) Downloaded 24 times
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Tree update issue?: tree.selectedItems()

Post by TheMarkster »

After add to the selection:

Code: Select all

Gui.updateGui()
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Tree update issue?: tree.selectedItems()

Post by Roy_043 »

I already tried that. It has no effect.
Post Reply