I am going through the Qt doc page on Mode-View architecture and while trying to reproduce the usage of model indexes in PyQt5 I got to a working script that I don’t fully understand. Here’s the code:
def main():
app = QApplication(sys.argv)
path = '/path/to/dir/'
model = QFileSystemModel()
model.setRootPath(path)
window = QWidget()
layout = QVBoxLayout(window)
label = QLabel("Files and Directories:n")
layout.addWidget(label)
@pyqtSlot(str)
def link_data(directory):
parent_index = model.index(directory)
num_rows = model.rowCount(parent_index)
names = []
for row in range(num_rows):
index = model.index(row, 0, parent_index)
name = model.data(index)
names.append(name)
label.setText("Files and Directories:n" + "n".join(names))
# Connect the directoryLoaded signal to the link_data slot
model.directoryLoaded.connect(link_data)
window.setWindowTitle("File System Model Data Retrieval")
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
It seems to me that model.directoryLoaded.connect(link_data)
will enusre that when the directoryLoaded
signal is emitted the root path of the model is passed to the link_data
slot. But how am I supposed to know that? The QFileSystemModel.py only says:
def directoryLoaded(self, *args, **kwargs): # real signature unknown
"""
pyqtSignal(*types, name: str = ..., revision: int = ..., arguments: Sequence = ...) -> PYQT_SIGNAL
types is normally a sequence of individual types. Each type is either a
type object or a string that is the name of a C++ type. Alternatively
each type could itself be a sequence of types each describing a different
overloaded signal.
name is the optional C++ name of the signal. If it is not specified then
the name of the class attribute that is bound to the signal is used.
revision is the optional revision of the signal that is exported to QML.
If it is not specified then 0 is used.
arguments is the optional sequence of the names of the signal's arguments.
"""
pass
And the C++ docs only say that void QFileSystemModel::directoryLoaded(const QString &path)
is emitted when the gatherer thread has finished to load the path.
Can someone clarify how I can find this type of information in the docs and how I can refactor my python code to make this behaviour more evident?