简体   繁体   English

无法在TreeView PyQt4中单击获取项目?

[英]Unable to grab the item on click in TreeView PyQt4?

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 当我单击树视图中的字符串/项时,它显示: Pyqt4.QtCore.QModelIndex对象位于0xb6b6c7d4而不是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_())

Easy fix. 轻松修复。 Your signal returns a QModelIndex, but you need to lookup up the item for that index in your model using itemFromIndex : 您的信号返回QModelIndex,但您需要使用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+ 虽然我有领奖台,但我想提到连接信号和插槽的非常棒的新方法,只要你使用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. 它完全是对象风格,您可以直接访问信号对象,并告诉它可连接的可调用插槽。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM