2014-05-19 24 views
1

我是一個Python粉絲和一個新手。我發現當我使用InternalPointer獲取文本(項目)在QTreeView可以發生崩潰,所以我在谷歌搜索解決方法,我發現InternalId,但它返回int,但我想用它來獲取文本,呃我不知道如何使用它。 我折騰了很久,但真的不明白,所以我想請你幫我解決這個問題。 希望能夠簡單易懂:) 非常感謝!如何使用InternalId在PyQt4的QTreeView中獲取文本

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_()) 

回答

1

可以使用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. 
+0

解決,謝謝! – huyuanqi

相關問題