Grégoire Borel
Grégoire Borel

Reputation: 2028

Add external QML module path to QQmlApplicationEngine

I have an external QML module composed of graphical elements. I'd like to find its path in order to add it to my QQmlApplicationEngine. Is there a way to do this?

QQmlApplicationEngine engine;

engine.addImportPath("externalQmlModulePath");

With this, I'll be able to import the graphical elements from my qrc's QML files (which are of course inside the project).

Upvotes: 2

Views: 2023

Answers (1)

qCring
qCring

Reputation: 1442

Take a look at QStandardpaths. Having your resources (whether your own or 3rd party) relative to those paths makes them consistently available on target systems. The suggested path for application specific data is QStandardPaths::AppDataLocation.

In CMake you could add a custom post-build command to copy all your resources (again, no difference if your own or 3rd party):

add_custom_command(TARGET ${MY_APP} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${MY_APP_RES_SOURCE_DIR} ${MY_APP_RES_DEST_DIR})

Edit

QStandardPaths::AppDataLocation is of course just an enum value to specify which standard path you are looking for. To actually get the app data path, use the standardLocations method like this:

auto appDataPath = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).first();

Finally, add your app's resource folder as import path (as you already did) and you're done:

engine.addImportPath(appDataPath + "/res_dir_name");

Note: On Mac you can get away more easily by putting resources in the application bundle.

Upvotes: 2

Related Questions