How to import custom qml components via import <Module>.<Submodule> statements?>?
-
I've written a custom qml UI library named
KmcUIfor instance. Here is directory strucuture.Myproject |- KmcUI |- Controls |- MyComponent.qml |- qmldir |- main.qmlAnd here is the content of
qmldirmodule Kmc.Controls MyComponent 1.0 MyComponent.qmlI also set
QML_IMPORT_PATHto$$PWD/KmcUIin.profile.
However, when I useimport KmcUI.Controlsinmain.qml, it still showsQML module not found. -
I finally found the solution!!! Everyone who is confused by Qt documentation should read my answer.
You should do the following:
- Write
qmldirfor every sub directory, just like I described in question. - Add all of the
qmldirs and.qmlfiles to a.qrcfile that located in the outermost directory, in my case, is.\KmcUi. Don't forget to change the default prefix\to the module name, in my case, isKmcUI. - Create a
.prifile, just include the.qrcjust created in step 3. This step is optional, the most important is ensuring add.qrcin your.pro. - In your own
.profile, include.prior.qrccreated in step 3 or step 2. - Call
engine.addImportPath("qrc:/"). You can also add following line to your.profile. Then useINSTALL_PATH(engine).
DEFINES += INSTALL_PATH\\(E\\)=\"E.addImportPath(QStringLiteral(\\\"qrc:/\\\"));\"- Import your module in your own qml file!
- Write
-
I've written a custom qml UI library named
KmcUIfor instance. Here is directory strucuture.Myproject |- KmcUI |- Controls |- MyComponent.qml |- qmldir |- main.qmlAnd here is the content of
qmldirmodule Kmc.Controls MyComponent 1.0 MyComponent.qmlI also set
QML_IMPORT_PATHto$$PWD/KmcUIin.profile.
However, when I useimport KmcUI.Controlsinmain.qml, it still showsQML module not found. -
I finally found the solution!!! Everyone who is confused by Qt documentation should read my answer.
You should do the following:
- Write
qmldirfor every sub directory, just like I described in question. - Add all of the
qmldirs and.qmlfiles to a.qrcfile that located in the outermost directory, in my case, is.\KmcUi. Don't forget to change the default prefix\to the module name, in my case, isKmcUI. - Create a
.prifile, just include the.qrcjust created in step 3. This step is optional, the most important is ensuring add.qrcin your.pro. - In your own
.profile, include.prior.qrccreated in step 3 or step 2. - Call
engine.addImportPath("qrc:/"). You can also add following line to your.profile. Then useINSTALL_PATH(engine).
DEFINES += INSTALL_PATH\\(E\\)=\"E.addImportPath(QStringLiteral(\\\"qrc:/\\\"));\"- Import your module in your own qml file!
- Write
-
K Kamichanw has marked this topic as solved on