2014-01-23 49 views
0

我得到POO,python和PyQt慢,我有一个问题,以了解有关传递参数和属性的东西。QTreeView,类,参数,实例和属性

我在网上找到了一个处理QTreeView的代码(见下面),我不明白Index是如何传入showPath()的。另外为什么self.filename和self.filepath不会传递给实例?

我希望我已经够清楚了......非常感谢。

from PyQt4   import QtGui 
class TreeViewWidget(QtGui.QWidget): 
def __init__(self, parent=None): 

    super(TreeViewWidget, self).__init__(parent)     
    self.model = QtGui.QFileSystemModel(self) 
    self.model.setRootPath(rootpath) 
    self.indexRoot = self.model.index(self.model.rootPath()) 

    self.treeView = QtGui.QTreeView(self) 
    self.treeView.setExpandsOnDoubleClick(False) 
    self.treeView.setModel(self.model) 
    self.treeView.setRootIndex(self.indexRoot) 
    self.treeView.setColumnWidth(0,220) 
    self.treeView.clicked.connect(self.showPath) 
    self.treeView.doubleClicked.connect(self.openQuickLook) 

    self.labelFileName = QtGui.QLabel(self) 
    self.labelFileName.setText("File Name:") 

    self.lineEditFileName = QtGui.QLineEdit(self) 

    self.labelFilePath = QtGui.QLabel(self) 
    self.labelFilePath.setText("File Path:") 

    self.lineEditFilePath = QtGui.QLineEdit(self) 

    self.gridLayout = QtGui.QGridLayout() 
    self.gridLayout.addWidget(self.labelFileName, 0, 0) 
    self.gridLayout.addWidget(self.lineEditFileName, 0, 1) 
    self.gridLayout.addWidget(self.labelFilePath, 1, 0) 
    self.gridLayout.addWidget(self.lineEditFilePath, 1, 1) 

    self.layout = QtGui.QVBoxLayout(self) 
    self.layout.addLayout(self.gridLayout) 
    self.layout.addWidget(self.treeView) 

def givePathName(self, index): 

    indexItem = self.model.index(index.row(), 0, index.parent()) 

    self.filename = self.model.fileName(indexItem) 
    self.filepath = self.model.filePath(indexItem) 

def showPath(self, index): 

    self.givePathName(index) 
    self.lineEditFileName.setText(self.filename) 
    self.lineEditFilePath.setText(self.filepath) 

回答

2

我不明白怎么index传递到showPath()

你widget的点击信号连接到showPath明确:这个信号的

self.treeView.clicked.connect(self.showPath) 

部分是具体的index项目点击;这会自动作为参数传递给showPath

另外为什么self.filenameself.filepath没有传递给实例?

他们实例属性,他们属于实例,是到该实例的所有方法访问。它们在givePathName()中创建,然后成为TreeViewWidget实例对象的一部分。它们以self.开头,因为按惯例,这是实例方法中实例的名称(以及这些方法的隐式第一个参数)。

把它们一起:

def showPath(self, index): 
      #^the instance object, so you can access its attributes 
       #^the index of the specific item clicked