PySide2 - 调用 QFileSystemModel.index() 时程序崩溃
PySide2 - Program crashes when calling QFileSystemModel.index()
我正在尝试将 https://doc.qt.io/qt-5/model-view-programming.html#using-model-indexes 中的以下 C++ 示例转换为 Python:
C++:
QFileSystemModel *model = new QFileSystemModel;
QModelIndex parentIndex = model->index(QDir::currentPath());
int numRows = model->rowCount(parentIndex);
Python:
import os
from PySide2.QtWidgets import *
model = QFileSystemModel()
parent_index = model.index(os.getcwd())
nb_row = model.rowCount(parent_index)
print(nb_row)
但我的程序崩溃并显示退出代码:
Process finished with exit code -1073741819 (0xC0000005)
如果您 运行 CMD/console 中的代码,您将收到以下错误消息:
QSocketNotifier: Can only be used with threads started with QThread
Segmentation fault (core dumped)
表示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
:
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 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. The fileInfo(), isDir(),
fileName() and filePath() functions provide information about the
underlying files and directories related to items in the model.
Directories can be created and removed using mkdir(), rmdir().
Note: QFileSystemModel requires an instance of QApplication.
(强调我的)
对于 C++,QApplication 可能是在 main.cpp 中创建的。
我正在尝试将 https://doc.qt.io/qt-5/model-view-programming.html#using-model-indexes 中的以下 C++ 示例转换为 Python:
C++:
QFileSystemModel *model = new QFileSystemModel;
QModelIndex parentIndex = model->index(QDir::currentPath());
int numRows = model->rowCount(parentIndex);
Python:
import os
from PySide2.QtWidgets import *
model = QFileSystemModel()
parent_index = model.index(os.getcwd())
nb_row = model.rowCount(parent_index)
print(nb_row)
但我的程序崩溃并显示退出代码:
Process finished with exit code -1073741819 (0xC0000005)
如果您 运行 CMD/console 中的代码,您将收到以下错误消息:
QSocketNotifier: Can only be used with threads started with QThread
Segmentation fault (core dumped)
表示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
:
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 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. The fileInfo(), isDir(), fileName() and filePath() functions provide information about the underlying files and directories related to items in the model. Directories can be created and removed using mkdir(), rmdir().
Note: QFileSystemModel requires an instance of QApplication.
(强调我的)
对于 C++,QApplication 可能是在 main.cpp 中创建的。