`QML_IMPORT_PATH` has no effect
-
The issue
I was trying to follow a basic tutorial for creating your own QML components library when I ran into (yet another) issue trying to import identified QML modules. As a very simple example for demonstration I have the following folder in my file system:
qmldir:
module Buttons CustomButton 1.0 CustomButton_01.qml
CustomButton_01.qml:
import QtQuick Rectangle { anchors.fill: parent color: "black" }
Now I just want to import this
Buttons
module in a project so that I can test and adjust the component. Of course I need to make the QML engine aware of where to look for the module and the best way to do that is theQML_IMPORT_PATH
environment variable as described by the official documentation. So the best way to do that is by modifyingCMakeLists.txt
as follows:cmake_minimum_required(VERSION 3.16) project(ComponentCreator VERSION 0.1 LANGUAGES CXX) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 6.5 REQUIRED COMPONENTS Quick) qt_standard_project_setup(REQUIRES 6.5) ###################### QML_IMPORT_PATH ###################### # Doing it both with '\' and '/' just to demonstrate that's not the issue list(APPEND QML_DIRS "C:/Qt/CustomComponents") list(APPEND QML_DIRS "C:\\Qt\\CustomComponents") set(QML_IMPORT_PATH "${QML_DIRS}" CACHE STRING "Qt Creator QML import paths" FORCE) ###################### QML_IMPORT_PATH ###################### qt_add_executable(appComponentCreator main.cpp ) qt_add_qml_module(appComponentCreator URI ComponentCreator VERSION 1.0 QML_FILES Main.qml ) # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. # If you are developing for iOS or macOS you should consider setting an # explicit, fixed bundle identifier manually though. set_target_properties(appComponentCreator PROPERTIES # MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appComponentCreator MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) target_link_libraries(appComponentCreator PRIVATE Qt6::Quick ) include(GNUInstallDirs) install(TARGETS appComponentCreator BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
Now trying to use the module in
Main.qml
:import QtQuick import Buttons # Produces 2 warnings Window { width: 640 height: 480 visible: true title: qsTr("Hello World") }
Not only do I get warnings in the Qt Creator for that import:
C:\Users\Nigyzst\source\repos\Qt\ComponentCreator\Main.qml:2: warning: Warnings occurred while importing module "Buttons": [import] C:\Users\Nigyzst\source\repos\Qt\ComponentCreator\Main.qml:1: warning: Failed to import Buttons. Are your import paths set up properly? [import]
But surprisingly the warnings are correct as running the application fails:
Double checking the CMake configuration after running and it seems fine:
Question
What am I doing wrong that the both the QML engine (at runtime) and the Language Server (in the IDE) can't find the module? I have read the official documentation and looked around on Stackoverflow but according to both resources it should work.
Environment
- Windows 10 64-bit
- Qt Creator 14.0.2 (Community): all settings completely untouched
- Qt 6.8.0
- Project: Qt Quick Application, Kit: Destkop Qt 6.8.0 MinGW 64-bit with CMake
-
try this:
use qt_standard_project_setup(REQUIRES 6.8)
this will enable policy QTP004 (https://doc.qt.io/qt-6/qt-cmake-policy-qtp0004.html)then you don 't have to deal with import pathes, you don't have to create qmldir files - qmlls should find your QML files inside folders.
check your QtC preferences QtQuick - Enable JS/QML Editing: check QMLLS settings
also you can try to set QT_QML_GENERATE_QMLLS_INI fir CMake -
try this:
use qt_standard_project_setup(REQUIRES 6.8)
this will enable policy QTP004 (https://doc.qt.io/qt-6/qt-cmake-policy-qtp0004.html)then you don 't have to deal with import pathes, you don't have to create qmldir files - qmlls should find your QML files inside folders.
check your QtC preferences QtQuick - Enable JS/QML Editing: check QMLLS settings
also you can try to set QT_QML_GENERATE_QMLLS_INI fir CMake@ekkescorner Thanks for the effort.
Shortly, I tried everything you suggested but everything remained the same. Here is what exactly I did to confirm:
- Replace
qt_standard_project_setup(REQUIRES 6.5)
withqt_standard_project_setup(REQUIRES 6.8)
in the CMake file - Rerun, see if anything changed: nothing
- Check Qt Creator preferences, seem fine as they were, didn't change anything here:
- Added
set(QT_QML_GENERATE_QMLLS_INI ON)
in CMake. - Rerun and see that nothing changed.
I am not surprised
set(QT_QML_GENERATE_QMLLS_INI ON)
didn't do anything as that helps only if the module I am importing is a part of the project and gets put in the build directory, but here it's not. Not sure why butQTP004
also didn't change anything. - Replace
-
sorry - have overlooked that your module isn't part of the project.
my experiences with nested QML folders are with folders inside the project, where the settings I mentioned make differences with 6.8
ATM my projects (ported from 5.15 to 6.6...6.8) are using QMake, because I'm waiting for some fixes with 6.8.1 (end of nov '24) before doing the ports from QMake to CMake.
this means it'll take some months until I'll run into same situations - it's on my TODO to separate common QML files to be used in my apps. From my understanding that will be extra qml_modules I have to import or link.
but no time to test this now... -
sorry - have overlooked that your module isn't part of the project.
my experiences with nested QML folders are with folders inside the project, where the settings I mentioned make differences with 6.8
ATM my projects (ported from 5.15 to 6.6...6.8) are using QMake, because I'm waiting for some fixes with 6.8.1 (end of nov '24) before doing the ports from QMake to CMake.
this means it'll take some months until I'll run into same situations - it's on my TODO to separate common QML files to be used in my apps. From my understanding that will be extra qml_modules I have to import or link.
but no time to test this now...@ekkescorner Alright thanks a lot for the info. Just one question: how can I switch to usung qmake in my Qt Quick app because when I create a new project from the Qt Creator, it doesn't ask me to choose between qmake and CMake? I suppose I can manually create a
.pro
file and open it with Qt Creator or? -
Yep. Create a .pro and open from QtC