Implement QFileSystemModel in CLi
-
Hello.
I am trying to use QFileSystemModel in a applciation that has both GUI and CLI in order to monitor if files are created in a directory. I have the following code:
For CLI part I have tried to run the following code (in a separate project).
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); // create the main class QFileSystemModel model; model.setRootPath("D:/"); return app.exec(); }
When the root path is set the app throws a Access violation reading.
Is there any chance that QFileSystemModel cannot be used under CLI? If I am using the class in a GUI application that has QApplication on main everything works.
Any suggestions?
Thank you -
Hi and welcome to devnet,
Can you show the stack trace ?
Which version of Qt are you using ?
How are you starting your application ? -
@MihEdd said in Implement QFileSystemModel in CLi:
Is there any chance that QFileSystemModel cannot be used under CLI?
Documentation is your friend:
Note: QFileSystemModel requires an instance of QApplication.
-
@SGaist
Here is my stack. I am using Qt 5.14.1.
```
[Inline Frame] Qt5Widgets.dll!QFileIconProviderPrivate::getIcon(const QFileInfo &) Line 236
at c:\users\qt\work\qt\qtbase\src\widgets\itemviews\qfileiconprovider.cpp(236)
Qt5Widgets.dll!QFileIconProvider::icon(const QFileInfo & info) Line 247
at c:\users\qt\work\qt\qtbase\src\widgets\itemviews\qfileiconprovider.cpp(247)
Qt5Widgets.dll!QFileInfoGatherer::getInfo(const QFileInfo & fileInfo) Line 331
at c:\users\qt\work\qt\qtbase\src\widgets\dialogs\qfileinfogatherer.cpp(331)
Qt5Widgets.dll!QFileSystemModelPrivate::node(const QString & path, bool fetch) Line 478
at c:\users\qt\work\qt\qtbase\src\widgets\dialogs\qfilesystemmodel.cpp(478)
[Inline Frame] Qt5Widgets.dll!QFileSystemModelPrivate::index(const QString & path, int) Line 229
at c:\users\qt\work\qt\qtbase\src\widgets\dialogs\qfilesystemmodel_p.h(229)
Qt5Widgets.dll!QFileSystemModel::setRootPath(const QString & newPath) Line 1517
at c:\users\qt\work\qt\qtbase\src\widgets\dialogs\qfilesystemmodel.cpp(1517)
Synoptes-CLI.exe!main(int argc, char * * argv) Line 33
at D:\Projects RISE\Synoptes\Qt Repo\Synoptes-GUI\Synoptes-CLI\main.cpp(33)
[External Code] -
It's because of what @Pablo-J-Rogina wrote: you shall use a QApplication in order to use QFileSystemModel.
-
Thank you for the help.
If I am using QApplication instead of QCoreApplication the CLI is not working. I tried to move the QFileSystemModel in a separate thread, and in the separate thread I created a instance of QApplication. The QFileSystemModel works, but on main() the application fails when returning app->exec(). It seems that in cannot use QApplication beacuse is related with the GUI. Is QFileSystemModel available only for GUI applications?
-
@MihEdd said in Implement QFileSystemModel in CLi:
Is QFileSystemModel available only for GUI applications?
I only know that
QFileSystemModel
uses a separate thread to populate itself so it will not cause the main thread to hang as the file system is being queried. Since @Pablo-J-Rogina has drawn your attention toNote: QFileSystemModel requires an instance of QApplication.
one presumes that is indeed the case.
-
What do you mean by "CLI is not working" now ? The code you posted just created the model and starts the event loop. What are you expecting it to do ?