OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How do I know what a pyqtSignal will pass to a pyqtSlot?

  • Thread starter Thread starter curioso
  • Start date Start date
C

curioso

Guest
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:

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)

    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.

The C++ docs only say that void QFileSystemModel::directoryLoaded(const QString &path) is emitted when the gatherer thread has finished to load the path.

I want to understand how the directory argument from link_data(directory) gets filled, and how I can figure that from the docs.
<p>I am going through the Qt <a href="https://doc.qt.io/qt-5/model-view-programming.html" rel="nofollow noreferrer">doc page on Mode-View architecture</a> and while trying to reproduce the <a href="https://doc.qt.io/qt-5/model-view-programming.html#using-model-indexes" rel="nofollow noreferrer">usage of model indexes</a> in PyQt5 I got to a working script that I don't fully understand. Here's the code:</p>
<pre class="lang-py prettyprint-override"><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)

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()
</code></pre>
<p>It seems to me that <code>model.directoryLoaded.connect(link_data)</code> will enusre that, when the <code>directoryLoaded</code> signal is emitted, the root path of the model is passed to the <code>link_data</code> slot.</p>
<p>The <a href="https://doc.qt.io/qt-5/qfilesystemmodel.html#directoryLoaded" rel="nofollow noreferrer">C++ docs</a> only say that <code>void QFileSystemModel::directoryLoaded(const QString &path)</code> is emitted when the gatherer thread has finished to load the path.</p>
<p><strong>I want to understand how the <code>directory</code> argument from <code>link_data(directory)</code> gets filled, and how I can figure that from the docs.</strong></p>
 

Latest posts

Top