简体   繁体   English

PySide2 - 调用 QFileSystemModel.index() 时程序崩溃

[英]PySide2 - Program crashes when calling QFileSystemModel.index()

I am trying to convert into Python the following C++ example from https://doc.qt.io/qt-5/model-view-programming.html#using-model-indexes :我正在尝试将https://doc.qt.io/qt-5/model-view-programming.html#using-model-indexes 中的以下 C++ 示例转换为 Python:

C++: C++:

QFileSystemModel *model = new QFileSystemModel;
QModelIndex parentIndex = model->index(QDir::currentPath());
int numRows = model->rowCount(parentIndex);

Python: Python:

import os
from PySide2.QtWidgets import *
model = QFileSystemModel()
parent_index = model.index(os.getcwd())
nb_row = model.rowCount(parent_index)
print(nb_row)

but my progam crashes with exit code :但我的程序因退出代码而崩溃:

Process finished with exit code -1073741819 (0xC0000005)

If you run the code in a CMD/console you will get the following error message:如果您在 CMD/控制台中运行代码,您将收到以下错误消息:

QSocketNotifier: Can only be used with threads started with QThread
Segmentation fault (core dumped)

Which indicates that QFileSystemModel uses a QThread (that is also indicated in the docs ), and for a QThread to run it needs an event loop, in this case you must create an instance of QApplication :这表明QFileSystemModel使用 QThread (也在docs 中指出),并且 QThread 运行它需要一个事件循环,在这种情况下,您必须创建QApplication的实例:

import os
import sys

from PySide2.QtWidgets import QApplication, QFileSystemModel

if __name__ == "__main__":

    app = QApplication(sys.argv)
    model = QFileSystemModel()
    parent_index = model.index(os.getcwd())
    nb_row = model.rowCount(parent_index)
    print(nb_row)

The above is also explicitly indicated in the docs :文档中也明确指出了上述内容

Detailed Description详细说明

This class provides access to the local filesystem, providing functions for renaming and removing files and directories, and for creating new directories.此类提供对本地文件系统的访问,提供重命名和删除文件和目录以及创建新目录的功能。 In the simplest case, it can be used with a suitable display widget as part of a browser or filter.在最简单的情况下,它可以与合适的显示小部件一起使用,作为浏览器或过滤器的一部分。

QFileSystemModel can be accessed using the standard interface provided by QAbstractItemModel, but it also provides some convenience functions that are specific to a directory model. QFileSystemModel 可以使用 QAbstractItemModel 提供的标准接口进行访问,但它也提供了一些特定于目录模型的便利功能。 The fileInfo(), isDir(), fileName() and filePath() functions provide information about the underlying files and directories related to items in the model. fileInfo()、isDir()、fileName() 和 filePath() 函数提供有关与模型中的项目相关的基础文件和目录的信息。 Directories can be created and removed using mkdir(), rmdir().可以使用 mkdir()、rmdir() 创建和删除目录。

Note: QFileSystemModel requires an instance of QApplication.注意:QFileSystemModel 需要一个 QApplication 的实例。

(emphasis mine) (强调我的)

In the case of C++, QApplication was probably created in the main.cpp.在 C++ 的情况下,QApplication 可能是在 main.cpp 中创建的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Pyside2 QThread 线程时 Scipy curve_fit 崩溃 - Scipy curve_fit crashes when threaded with Pyside2 QThread 当 qcombobox 索引更改覆盖 QUiloader 时,PySide2 在小部件上重新绘制 - PySide2 repaint on widget when qcombobox index changes overriding QUiloader PySide2 在访问 QObject::property() 时崩溃 - PySide2 crashes while accessing QObject::property() 在 PySide2 虚拟环境中调用 lrelease 时如何修复“无法识别为内部或外部命令”? - How to fix “Not Recognized As an Internal or External Command” when calling lrelease in PySide2 virtual environment? 在 PyQT5/Pyside2 中获取所选行的索引 - Getting Index of selected row in PyQT5/ Pyside2 PySide2/QML 在给定的根索引处过滤分层数据 - PySide2/QML Filtering hierarchical data at given root index 导入 PySide2 时 DLL 加载失败,仅在调试时 - DLL load failed when importing PySide2, only while debugging 使用 Qt Designer 时显示 PySide2 QCharts - Getting PySide2 QCharts to show up when using Qt Designer 具有Qt Designer的Pyside2-使用QUiLoader()时忽略资源(图标) - Pyside2 with Qt Designer - Resource(icons) ignored when using QUiLoader() 当 QRunnable 仍在运行时,防止 PySide2 对话框关闭 - Prevent PySide2 dialog from closing when QRunnable still running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM