Usage of IMPORT_PATH in qt_add_qml_module
-
I can't seem to get the IMPORT_PATH given in qt_add_qml_module to work. Here is what I am trying do do:
I use cmake and qt_add_qml_module to add multiple static QML modules in my project (Qt 6.5.1). Some of these modules depend on each other.
So let's say I have QML module "MyCompany.Abc" which depends on "MyCompany.Core".
In the qt_add_qml_module for "MyCompany.Abc" I addIMPORTS "MyCompany.Core"
However, when editing a QML file in "MyCompany.Abc", I get
"Implicit import 'MyCompany.Core' or QML module 'MyCompany.Abc' not found"It also mentions that the QML import path may be missing, and shows that only '<Qt install path>/qml' is in the import paths.
I thought this should be easy to fix by adding IMPORT_PATH to qt_add_qml_module. So I added the root path of all my QML modules (located in the build path, because that's where the QML modules get generated). But that seems to be totally ignored:
- It doesn't fix the error I see in QML editor (and no additional import path is shown in the error message)
- The path doesn't seem to be added to the generated qmldir file
How do I use IMPORT_PATH correctly?
-
I can't seem to get the IMPORT_PATH given in qt_add_qml_module to work. Here is what I am trying do do:
I use cmake and qt_add_qml_module to add multiple static QML modules in my project (Qt 6.5.1). Some of these modules depend on each other.
So let's say I have QML module "MyCompany.Abc" which depends on "MyCompany.Core".
In the qt_add_qml_module for "MyCompany.Abc" I addIMPORTS "MyCompany.Core"
However, when editing a QML file in "MyCompany.Abc", I get
"Implicit import 'MyCompany.Core' or QML module 'MyCompany.Abc' not found"It also mentions that the QML import path may be missing, and shows that only '<Qt install path>/qml' is in the import paths.
I thought this should be easy to fix by adding IMPORT_PATH to qt_add_qml_module. So I added the root path of all my QML modules (located in the build path, because that's where the QML modules get generated). But that seems to be totally ignored:
- It doesn't fix the error I see in QML editor (and no additional import path is shown in the error message)
- The path doesn't seem to be added to the generated qmldir file
How do I use IMPORT_PATH correctly?
I find IMPORT_PATH, IMPORTS, OPTIONAL_IMPORTS complicated to understand.
As explained here
'If a QML file imports this module, it also imports all the modules listed under IMPORTS.'
My understanding is that this should only be used to tell the engine that if someone import the root module is also implicitly importing its imports. It is like public linking in CMake.
One should not use IMPORTS only because your root module import other module.
I think your problem could be related to not adding the build path of the module to the 'QML_IMPORT_PATH' variable of CMake that qtCreator use for searching the modules.Normally what i do in cmake is to append my module paths to this variable like
list(APPEND QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/qml ${CMAKE_CURRENT_BINARY_DIR}/) list(REMOVE_DUPLICATES QML_IMPORT_PATH) set(QML_IMPORT_PATH ${QML_IMPORT_PATH} CACHE STRING "" FORCE)
This depends on the layout of your modules. I just add the path to the module qml files and the build directory where the module is built.
-
I find IMPORT_PATH, IMPORTS, OPTIONAL_IMPORTS complicated to understand.
As explained here
'If a QML file imports this module, it also imports all the modules listed under IMPORTS.'
My understanding is that this should only be used to tell the engine that if someone import the root module is also implicitly importing its imports. It is like public linking in CMake.
One should not use IMPORTS only because your root module import other module.
I think your problem could be related to not adding the build path of the module to the 'QML_IMPORT_PATH' variable of CMake that qtCreator use for searching the modules.Normally what i do in cmake is to append my module paths to this variable like
list(APPEND QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/qml ${CMAKE_CURRENT_BINARY_DIR}/) list(REMOVE_DUPLICATES QML_IMPORT_PATH) set(QML_IMPORT_PATH ${QML_IMPORT_PATH} CACHE STRING "" FORCE)
This depends on the layout of your modules. I just add the path to the module qml files and the build directory where the module is built.
@Mesrine
In the end, I use a variant of set(QML_IMPORT_PATH...) similar to what you suggested.
It seems that if I add the same path via set(QML_IMPORT_PATH...) it works. If I add the path via qt_add_qml_module (parameter IMPORT_PATH ) it doesn't.I don't feel like writing the (rather complicated) sample project that would be needed in order to file this as a bug, so I guess I'll be happy to have a way that works...
EDIT: It seems that not a single of Qt's examples uses the qt_add_qml_module with IMPORT_PATH. Tells a lot...
-
I can't seem to get the IMPORT_PATH given in qt_add_qml_module to work. Here is what I am trying do do:
I use cmake and qt_add_qml_module to add multiple static QML modules in my project (Qt 6.5.1). Some of these modules depend on each other.
So let's say I have QML module "MyCompany.Abc" which depends on "MyCompany.Core".
In the qt_add_qml_module for "MyCompany.Abc" I addIMPORTS "MyCompany.Core"
However, when editing a QML file in "MyCompany.Abc", I get
"Implicit import 'MyCompany.Core' or QML module 'MyCompany.Abc' not found"It also mentions that the QML import path may be missing, and shows that only '<Qt install path>/qml' is in the import paths.
I thought this should be easy to fix by adding IMPORT_PATH to qt_add_qml_module. So I added the root path of all my QML modules (located in the build path, because that's where the QML modules get generated). But that seems to be totally ignored:
- It doesn't fix the error I see in QML editor (and no additional import path is shown in the error message)
- The path doesn't seem to be added to the generated qmldir file
How do I use IMPORT_PATH correctly?
@Asperamanca said in Usage of IMPORT_PATH in qt_add_qml_module:
It doesn't fix the error I see in QML editor
It is not intuitive indeed. But the setting IMPORT_PATH has no effect on your editor because:
QML_IMPORT_PATH
is for the editorIMPORT_PATH
is for the build-time tools (such as qmllint or qmlcachegen)
@Mesrine said in Usage of IMPORT_PATH in qt_add_qml_module:
Normally what i do in cmake is to append my module paths to this variable like
list(APPEND QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/qml ${CMAKE_CURRENT_BINARY_DIR}/) list(REMOVE_DUPLICATES QML_IMPORT_PATH) set(QML_IMPORT_PATH ${QML_IMPORT_PATH} CACHE STRING "" FORCE)
This depends on the layout of your modules. I just add the path to the module qml files and the build directory where the module is built.
Use this instead to avoid duplicates:
set(QT_QML_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/qml) set(QML_IMPORT_PATH ${QT_QML_OUTPUT_DIRECTORY} CACHE STRING "Import paths for Qt Creator's code model" FORCE)
It works with all kinds of layouts. Plus, you can now add
IMPORT_PATH ${QT_QML_OUTPUT_DIRECTORY}
to your modules and the tools will also work. -
@Asperamanca said in Usage of IMPORT_PATH in qt_add_qml_module:
It doesn't fix the error I see in QML editor
It is not intuitive indeed. But the setting IMPORT_PATH has no effect on your editor because:
QML_IMPORT_PATH
is for the editorIMPORT_PATH
is for the build-time tools (such as qmllint or qmlcachegen)
@Mesrine said in Usage of IMPORT_PATH in qt_add_qml_module:
Normally what i do in cmake is to append my module paths to this variable like
list(APPEND QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/qml ${CMAKE_CURRENT_BINARY_DIR}/) list(REMOVE_DUPLICATES QML_IMPORT_PATH) set(QML_IMPORT_PATH ${QML_IMPORT_PATH} CACHE STRING "" FORCE)
This depends on the layout of your modules. I just add the path to the module qml files and the build directory where the module is built.
Use this instead to avoid duplicates:
set(QT_QML_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/qml) set(QML_IMPORT_PATH ${QT_QML_OUTPUT_DIRECTORY} CACHE STRING "Import paths for Qt Creator's code model" FORCE)
It works with all kinds of layouts. Plus, you can now add
IMPORT_PATH ${QT_QML_OUTPUT_DIRECTORY}
to your modules and the tools will also work.@JKSH
Thank you! I kind of did what you suggested, but now I understand better, why. -