How to getting the application dir in QML file
-
Hi,
Check "resolvedUrl":http://qt-project.org/doc/qt-5/qml-qtqml-qt.html#resolvedUrl-method if its useful.
To get current directory try,
@
Qt.resolvedUrl(".")
@ -
Sorry but can you make more clear how you want the path ?
-
sorry for unclear statement, I checked the result ofQt.resolvedUrl("."), Its give s me qrc:/// and that's not I wanted, I want the absolute path of the application's executable file's containing directory. such as
@D:/path/to/demo.exe @
then I am looking for
@D:/path/to/@ -
You can use the baseUrl() reported by the engine.
-
Hello!
I know its an old post, but I came across and didn't find what I was looking for so I solved it like this:In your main.cpp add one new context property to your engine like this:
engine.rootContext()->setContextProperty("CurDirPath", QString(QDir::currentPath()));
Than use it on your Qml side like:
Component.onCompleted: { console.log("Your current dir path: " + CurDirPath) }
Hope it helps for future visitors.
-
Hello!
I know its an old post, but I came across and didn't find what I was looking for so I solved it like this:In your main.cpp add one new context property to your engine like this:
engine.rootContext()->setContextProperty("CurDirPath", QString(QDir::currentPath()));
Than use it on your Qml side like:
Component.onCompleted: { console.log("Your current dir path: " + CurDirPath) }
Hope it helps for future visitors.
@NapoLion
For anyone reading this: the question title asked for "application dir ". All solutions usingQDir::currentPath()
, or for that matterQDir()
, do not return that, they return the process's current/working directoryDepending on what .is meant by "the application dir", you may want
QCoreApplication::applicationDirPath()
or evenQFileInfo(argv[0]).dir().absolutePath()
, but I don't know how those fare with a QML application.