我是一个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/