QFileSystemModel crash when there is empty file
-
Hi, I'm building a file browser with QFileSystemModel. I found that the whole application crashed when there are empty files, like a .txt file with no content in it.
If the empty file is under the setRootPath path, the application would crash immediately when running. If the empty file is under deeper path, the application would run well and the file browser could be opened, but the application crashed when expanding the folder that the empty file is in.
Is there any way I can fix it? By setFilter or something else? I don't need the empty file to be displayed, just want to make QFileSystemModel work with possible existing empty files.
Thanks!
-
Hi,
Which version of Qt are you using ?
On which OS ? -
@ChellyP
Can you post a very minimal example? It would be really unusual for anything to cause a "crash", it's hard to imagine an empty file would do so. Also people have usedQFileSystemModelfor a long time, they will surely have had some empty files, it would be pretty serious/reported if that caused such a problem. -
@ChellyP
Can you post a very minimal example? It would be really unusual for anything to cause a "crash", it's hard to imagine an empty file would do so. Also people have usedQFileSystemModelfor a long time, they will surely have had some empty files, it would be pretty serious/reported if that caused such a problem.@JonB Thanks for your advice. I tested with the filesystembrowser example and found out the reason why
QFileSystemModelwith empty files crashed.Define application with
QApplication app(argc, argv);, everything works fine.But define with
QGuiApplication app(argc, argv);, file browser crashed when there are empty files.By empty files, it means files without extensions or with undefined extension and no content. For example, an empty text file with .txt extension (textFileName.txt) doesn't cause crash with
QGuiApplication, but an empty text file without .txt does crash the application.Anyway, thanks a lot for your help.
-
@JonB Thanks for your advice. I tested with the filesystembrowser example and found out the reason why
QFileSystemModelwith empty files crashed.Define application with
QApplication app(argc, argv);, everything works fine.But define with
QGuiApplication app(argc, argv);, file browser crashed when there are empty files.By empty files, it means files without extensions or with undefined extension and no content. For example, an empty text file with .txt extension (textFileName.txt) doesn't cause crash with
QGuiApplication, but an empty text file without .txt does crash the application.Anyway, thanks a lot for your help.
@ChellyP said in QFileSystemModel crash when there is empty file:
Define application with QApplication app(argc, argv);, everything works fine.
But define with QGuiApplication app(argc, argv);, file browser crashed when there are empty files.Interesting :)
QApplicationis supposed to be for a widget app,QGuiApplicationmore like for QML I think. Why the model goes wrong I do not know... I see https://stackoverflow.com/questions/49943635/qfilesystemmodel-crashing-when-seeting-root-path saysEdit: I realize now that the problem is related to the fact, that I am using a QGuiApplication. If I use a QApplication also homePath is working.
One issue is that QFileSystemModel requires a QApplication to run.. it says so in the detailed description
Note: QFileSystemModel requires an instance of QApplication.
Ah! https://doc.qt.io/qt-6/qfilesystemmodel.html#details says:
Note: QFileSystemModel requires an instance of QApplication.
So there is your answer :)