python - 如何使用InternalId获取PyQt4上QTreeView中的文本

标签 python pyqt4

我是一个Python爱好者,也是一个新手。我发现当我在QTreeView中使用InternalPointer get text(item)时会发生崩溃,所以我在google上搜索解决方法,我找到了InternalId,但它返回int,但我想用它来获取文本,呃我不知道如何使用它。 我折腾了好久,实在不明白,所以想请大家帮我解决一下这个问题。 希望简单易懂:) 非常感谢!

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

class TreeView(QtGui.QTreeView):
    def __init__(self, parent=None):
        super(TreeView, self).__init__(parent)          
        self.connect(self, SIGNAL("clicked(QModelIndex)"), self.getCurrentIndex)

    def getCurrentIndex(self, index):
        # Use 'InternalId' obtain the corresponding text, not int and hoping to simple.

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    model = QtGui.QDirModel()
    tree = TreeView()
    tree.setModel(model)
    tree.setWindowTitle(tree.tr("Dir View"))
    tree.resize(640, 480)
    tree.show()
    sys.exit(app.exec_())

最佳答案

您可以使用data method获取文本

此外,您可能更喜欢使用新样式的连接信号槽 docs

class TreeView(QtGui.QTreeView):
    def __init__(self, parent=None):
        super(TreeView, self).__init__(parent)      
        self.clicked.connect(self.getCurrentIndex)    
        # self.connect(self, SIGNAL("clicked(QModelIndex)"), self.getCurrentIndex)

    def getCurrentIndex(self, index):
        print(index.data())
        # Use 'InternalId' obtain the corresponding text, not int and hoping to simple.

关于python - 如何使用InternalId获取PyQt4上QTreeView中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23745511/

相关文章:

Python Nltk :UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 50: invalid continuation byte

python - 蛇与梯子,检查是否会降落在最后一个方 block 上

python - 在 QGraphicsGridLayout 中调整小部件大小

qt4 - PyQt 中的 paintEvent() 被不必要地调用

python - 如何使用 findChildren?

python - 将类标签附加到 Keras 模型

javascript - 有没有办法在编程语言中使用LaTex公式进行真正的计算

python - 如何检查是否按下了键盘修饰符(Shift、Ctrl 或 Alt)?

python - 使用 python3 将非 ASCII 字符传递给 PyQt4 的 tr() 国际化方法

python解析csv文件