Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the currently selected item in QTreeView

Tags:

python

qt

I have a number of items in a QTreeView. Each item is generated using this class:

class Branch(QStandardItem):
    def __init__(self, label, uri = None):
        QStandardItem.__init__(self, label)
        self.uri = uri

This is my actual tree:

class FileTree(QTreeView):
    def __init__(self):
        QTreeView.__init__(self)
    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Space or event.key() == Qt.Key_Return:
            crawler = self.selectedIndexes()[0].model().item(self.selectedIndexes()[0].row())
            print(crawler.uri)
        QTreeView.keyPressEvent(self, event)

As you can see, I'm a little unsure as to how to get the uri variable from the selected item. I found that selectedIndexes() returns a model and not the item itself. I'm not sure how to get from one to the other. Trying to get the item number using self.selectedIndexes()[0].row() was a bit of a shot in the dark, but it seems to ignore the various branches in the tree (for instance, it will give me a 0 for the first row in a branch, but won't tell me anything about what branch it's in).

What's the proper way to get the selected item from the QTreeView? Or is there a better way of detecting the spacebar or return keys being hit that would make this easier? There's a severe lack of Python documentation for Qt, so it's really hard to know if I'm ever doing things in a sensical manner.

like image 980
ashground Avatar asked Aug 12 '11 23:08

ashground


2 Answers

You are calling the right function, it actually returns a QModelIndexList which is just a typedef for QList<QModelIndex> with the QModelIndex being the data structure that can point to any part of the tree. QModelIndex is not a Model in the sense of Model View Controller (MVC) but an adress of an object in a QAbstractItemModel which is the datastructure under all of Qt's ItemView objects, including your tree. You are actually pretty close, QAbstractModelIndex consists of a row, a column and a parent, which lets it adress any position in a hierarchical data structure. If you use the line

index = self.selectedIndexes()[0]
crawler = index.model().itemFromIndex(index)

you should get to the object that you are looking for.

As for documentation, even though there is no python specific documentation it helps to read through the official Qt documentation, the class hierarchy and functionality is still the same. There is very little C++ specific information in the docs.

like image 118
Harald Scheirich Avatar answered Nov 10 '22 05:11

Harald Scheirich


Harald's answer didn't work for me, because I'm using a QSqlQueryModel as the model (I got the error {AttributeError}'QSqlQueryModel' object has no attribute 'itemFromIndex').

The below did the trick for me though, to get the 0th column data of the selected row:

dbQueryModel.itemData(treeView.selectedIndexes()[0])
like image 21
Taran Avatar answered Nov 10 '22 04:11

Taran