You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何确定PyQtSignal传递给pyqtSlot的参数?以QFileSystemModel为例

关于PyQt5中QFileSystemModel的directoryLoaded信号参数传递的疑问

我正在学习Qt的Model-View架构,用PyQt5复现模型索引用法时,写出了一段可运行但不完全理解的脚本,代码如下:

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()

我注意到model.directoryLoaded.connect(link_data)会在directoryLoaded信号触发时,将模型的根路径传递给link_data槽函数的directory参数。但查看QFileSystemModel的C++文档,仅说明void QFileSystemModel::directoryLoaded(const QString &path)会在后台线程完成路径加载时触发。

我想理解link_data(directory)中的directory参数是如何被填充的,以及如何从文档中获取这类信息。

内容的提问来源于stack exchange,提问作者curioso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 00:56:04