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

  • SEARCH
  • Home
  • 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 8229673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:52:36+00:00 2026-06-07T16:52:36+00:00

I am trying to grab the string/object from the treeview. So when a user

  • 0

I am trying to grab the string/object from the treeview. So when a user click on any item in the treeview, I can show it on the terminal. ANy help is appreciated.Here is the code. When I click the string/item in the treeview it shows this:
PyQt4.QtCore.QModelIndex object at 0xb6b6c7d4
instead of
Linux

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import QtCore, QtGui

data = root = [

    ("Linux", [

        ("System",

                [("System name",[]),
         ("Kernel", []),
         ("Drivers", []),
         ("Memory", []),
         ("Processes", []),
                 ("Disk mounted", []), 
         ("Services Running", []),
         ("Installed Packages", [])]),
        #[("System name", [])]),

        ("Network",
        [("Nework confi.",[]),
        ("Interface test", [])]),

        ("PCI Devices",
        [("PCI devices", [])]),

        ("Logs", 
        [("Messages",[]),
        ("Dmesg", [])]),


        ])]

class Window(QWidget):

    def __init__(self):

        QWidget.__init__(self)

        self.treeView = QTreeView()


        self.model = QStandardItemModel()
        self.addItems(self.model, data)
        self.treeView.setModel(self.model)

        self.model.setHorizontalHeaderLabels([self.tr("Object")])

        layout = QVBoxLayout()
        layout.addWidget(self.treeView)
        self.setLayout(layout)
    self.treeView.connect(self.treeView, QtCore.SIGNAL('clicked(QModelIndex)'), self.treefunction)

    def treefunction(self, index):
    print index


    def addItems(self, parent, elements):

        for text, children in elements:
            item = QStandardItem(text)
            parent.appendRow(item)
            if children:
                self.addItems(item, children)
if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
  • 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-06-07T16:52:38+00:00Added an answer on June 7, 2026 at 4:52 pm

    Easy fix. Your signal returns a QModelIndex, but you need to lookup up the item for that index in your model using itemFromIndex:

    def treefunction(self, index):
        print index.model().itemFromIndex(index).text()
        # print self.model.itemFromIndex(index).text()
    

    You can either get the model off the index, or specifically use your model attribute.

    And while I have the podium, I wanted to mention the really awesome new-style approach to connecting signals and slots, as long as you are using Qt 4.5+

    self.treeView.clicked.connect(self.treefunction)
    

    Notice how you don’t have to specify the string-based signature anymore. Its completely object-style where you access the signal object directly and just tell it the callable slot to connect with.

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

Sidebar

Related Questions

I am trying to grab the string off an object in a table row
I'm trying to grab the Web.UI.WebControls.HyperLink object itself via javascript so that I can
I am trying to grab the version number from a string via python regex...
Trying to grab the two $ values and the X value from this string
1 <span class='Txt9Gray'>Decisions ( </span> I'm trying to grab the 1 from this string.
I am trying to grab a user job notes but am not sure what
I have been trying to grab a file from an alternate git branch and
I'm trying to write a generic method to serialize an object that inherits from
Basically, I'm trying to grab an EXE from CNet's Download.com So i created web
I am trying to grab a number from a text box entry, convert it

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.