Python QTreeView、类、参数、实例和属性
我慢慢地接触到POO、python和PyQt,我很难理解有关传递参数和属性的内容 我在网上找到了一段处理QTreeView的代码(见下文),我不明白索引是如何传递到showPath()的。还有,为什么self.filename和self.filepath不传递给实例 我希望我足够清楚。。。非常感谢Python QTreeView、类、参数、实例和属性,python,class,pyqt,instance,qtreeview,Python,Class,Pyqt,Instance,Qtreeview,我慢慢地接触到POO、python和PyQt,我很难理解有关传递参数和属性的内容 我在网上找到了一段处理QTreeView的代码(见下文),我不明白索引是如何传递到showPath()的。还有,为什么self.filename和self.filepath不传递给实例 我希望我足够清楚。。。非常感谢 from PyQt4 import QtGui class TreeViewWidget(QtGui.QWidget): def __init__(self, parent=Non
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)
QTreeView的
单击的
信号将所单击项的QModelIndex
作为参数传递给任何连接的插槽
请参阅并。您的QTreeView的
单击的
信号将所单击项目的QModelIndex
作为参数传递给任何连接的插槽
请参阅并。您的QTreeView的
单击的
信号将所单击项目的QModelIndex
作为参数传递给任何连接的插槽
请参阅并。您的QTreeView的
单击的
信号将所单击项目的QModelIndex
作为参数传递给任何连接的插槽
见和
我不明白索引如何传递到showPath()
将小部件的单击信号显式连接到showPath
:
self.treeView.clicked.connect(self.showPath)
此信号的一部分是单击的特定项目的索引
;这将作为参数自动传递给showPath
还有,为什么不将self.filename
和self.filepath
传递给实例
它们是实例属性,属于该实例,并且该实例的所有方法都可以访问它们。它们是在givePathName()
中创建的,然后是TreeViewWidget
实例对象的一部分。它们以self.
开头,因为按照惯例,这是实例方法中给定给实例的名称(以及这些方法的隐式第一个参数)
综上所述:
def showPath(self, index):
# ^ the instance object, so you can access its attributes
# ^ the index of the specific item clicked
我不明白索引如何传递到showPath()
将小部件的单击信号显式连接到showPath
:
self.treeView.clicked.connect(self.showPath)
此信号的一部分是单击的特定项目的索引
;这将作为参数自动传递给showPath
还有,为什么不将self.filename
和self.filepath
传递给实例
它们是实例属性,属于该实例,并且该实例的所有方法都可以访问它们。它们是在givePathName()
中创建的,然后是TreeViewWidget
实例对象的一部分。它们以self.
开头,因为按照惯例,这是实例方法中给定给实例的名称(以及这些方法的隐式第一个参数)
综上所述:
def showPath(self, index):
# ^ the instance object, so you can access its attributes
# ^ the index of the specific item clicked
我不明白索引如何传递到showPath()
将小部件的单击信号显式连接到showPath
:
self.treeView.clicked.connect(self.showPath)
此信号的一部分是单击的特定项目的索引
;这将作为参数自动传递给showPath
还有,为什么不将self.filename
和self.filepath
传递给实例
它们是实例属性,属于该实例,并且该实例的所有方法都可以访问它们。它们是在givePathName()
中创建的,然后是TreeViewWidget
实例对象的一部分。它们以self.
开头,因为按照惯例,这是实例方法中给定给实例的名称(以及这些方法的隐式第一个参数)
综上所述:
def showPath(self, index):
# ^ the instance object, so you can access its attributes
# ^ the index of the specific item clicked
我不明白索引如何传递到showPath()
将小部件的单击信号显式连接到showPath
:
self.treeView.clicked.connect(self.showPath)
此信号的一部分是单击的特定项目的索引
;这将作为参数自动传递给showPath
还有,为什么不将self.filename
和self.filepath
传递给实例
它们是实例属性,属于该实例,并且该实例的所有方法都可以访问它们。它们是在givePathName()
中创建的,然后是TreeViewWidget
实例对象的一部分。它们以self.
开头,因为按照惯例,这是实例方法中给定给实例的名称(以及这些方法的隐式第一个参数)
综上所述:
def showPath(self, index):
# ^ the instance object, so you can access its attributes
# ^ the index of the specific item clicked