Getting the currently selected item in a QTreeView

I have a number of elements in a QTreeView. Each element is created 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 how to get the uri variable from the selected item. I found that the selectedIndexes()model returns, not the element itself. I am not sure how to get from one to another. Trying to get the position number with self.selectedIndexes()[0].row()was a little shot in the dark, but it seems to ignore the various branches of the tree (for example, it will give me the 0first line in the branch, but I will not tell me anything about which branch).

QTreeView? , ? Python Qt, , - .

+5
2

, QModelIndexList, typedef QList<QModelIndex>, QModelIndex , . QModelIndex Model View Controller (MVC), QAbstractItemModel, Item Item, . , QAbstractModelIndex , , .

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

, .

, python, Qt, . ++.

+5

Harald , QSqlQueryModel ( {AttributeError}'QSqlQueryModel' object has no attribute 'itemFromIndex').

, 0- :

dbQueryModel.itemData(treeView.selectedIndexes()[0])
0

All Articles