How to import custom qml components via import <Module>.<Submodule> statements?>?
Solved
QML and Qt Quick
-
I've written a custom qml UI library named
KmcUI
for instance. Here is directory strucuture.Myproject |- KmcUI |- Controls |- MyComponent.qml |- qmldir |- main.qml
And here is the content of
qmldir
module Kmc.Controls MyComponent 1.0 MyComponent.qml
I also set
QML_IMPORT_PATH
to$$PWD/KmcUI
in.pro
file.
However, when I useimport KmcUI.Controls
inmain.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
qmldir
for every sub directory, just like I described in question. - Add all of the
qmldir
s and.qml
files to a.qrc
file 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
.pri
file, just include the.qrc
just created in step 3. This step is optional, the most important is ensuring add.qrc
in your.pro
. - In your own
.pro
file, include.pri
or.qrc
created in step 3 or step 2. - Call
engine.addImportPath("qrc:/")
. You can also add following line to your.pro
file. Then useINSTALL_PATH(engine)
.
DEFINES += INSTALL_PATH\\(E\\)=\"E.addImportPath(QStringLiteral(\\\"qrc:/\\\"));\"
- Import your module in your own qml file!
- Write
-
I finally found the solution!!! Everyone who is confused by Qt documentation should read my answer.
You should do the following:
- Write
qmldir
for every sub directory, just like I described in question. - Add all of the
qmldir
s and.qml
files to a.qrc
file 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
.pri
file, just include the.qrc
just created in step 3. This step is optional, the most important is ensuring add.qrc
in your.pro
. - In your own
.pro
file, include.pri
or.qrc
created in step 3 or step 2. - Call
engine.addImportPath("qrc:/")
. You can also add following line to your.pro
file. Then useINSTALL_PATH(engine)
.
DEFINES += INSTALL_PATH\\(E\\)=\"E.addImportPath(QStringLiteral(\\\"qrc:/\\\"));\"
- Import your module in your own qml file!
- Write
-