Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6325715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:56:56+00:00 2026-05-24T16:56:56+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:56:57+00:00Added an answer on May 24, 2026 at 4:56 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a number of items coming in from a web service; each item
I have a class cnList which stores a flexible number of items. This list
I have a large number of Items in the core data. Each Item has
I have a following table using MVC that shows number of items the user
I have a list with items where each have number of properties (A, B,
So, I have a number of items clustered in OpenLayers. I'm using a strategy
I have a number of items in an array, each one associated to a
I have a number of items with different values. I'd like to represent each
I have a simple array with x number of items. I am displaying them
I have a windows form with a DropDownList with a fixed number of items.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.