Pergunta

I get to POO, python and PyQt slowly and I have a problem to understand something about passing argument and attributes.

I found online a code dealing with QTreeView (see below) and I don't understand how Index is passed into showPath(). Also why self.filename and self.filepath are not passed to the instance?

I hope I am clear enough ... Thank a lot.

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)
Foi útil?

Solução

I don't understand how index is passed into showPath()

You connect the widget's click signal to showPath explicitly:

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

part of this signal is the index of the specific item clicked on; this is passed as an argument to showPath automatically.

Also why self.filename and self.filepath are not passed to the instance?

They are instance attributes, they belong to the instance and are accessible to all methods of that instance. They are created in givePathName(), and are then part of the TreeViewWidget instance object. They start with self. because that is, by convention, the name given to the instance in instance methods (and the implicit first argument to those methods).

Putting that together:

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

Outras dicas

The clicked signal of your QTreeView pass the QModelIndex of the item clicked as an argument to any connected slots.

See Qt Documentation and PyQt signal and slots documentation.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top