QTreeWidget select first item

Viewed 8101

I am making a QTreeWidget from a XML file. The XML looks like below and I want to make a tree of the names:

<root>
    <f name='foo'>bar
        <f name='foo2'>baz</f>
    </f>
</root>

At the moment I am using the following code to do so (somewhat simplified code):

import lxml.etree as et

#...

self.xml = et.XML(filters.filtersxml)
self.tree_widget = QTreeWidget(parent)

def add_items(parent, xmlroot):
    for i in xmlroot.getchildren():
        item = QTreeWidgetItem(parent, [i.get('name')])
        if len(i.getchildren()) != 0:
            add_items(item, i)

add_items(self.tree_widget, self.xml)

I actually have two questions about this:

  1. Main question: Is there some way to select the first item in the tree, foo in this case. I tried to do something with setCurrentItem() and setCurrentIndex(), but couldn't get it to work. I have googled a bit about it, but all the solutions that I found work with models.
  2. (Optional) Is this recursive function a good approach to do this, or is there a better way?
1 Answers
Related