Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python QTreeView、类、参数、实例和属性_Python_Class_Pyqt_Instance_Qtreeview - Fatal编程技术网

Python QTreeView、类、参数、实例和属性

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

我慢慢地接触到POO、python和PyQt,我很难理解有关传递参数和属性的内容

我在网上找到了一段处理QTreeView的代码(见下文),我不明白索引是如何传递到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)

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