qtcreator qml component directories with cmake
-
I am trying to convert one last project from qmake to cmake and encountering a change in how qml files are handles.
When using qmake, custom components in a different directory are found without anyimport "directory"
statements.
When using cmake, the project still builds and runs fine, but within QtCreator custom components are marked in red withUnknown component. (M300)
. If I follow https://doc.qt.io/qt-5/qtqml-syntax-directoryimports.html and explicitly import the directory, then the editor is happy, but the build fails to run with error "no such directory".If possible, I would like to retain the qmake behavior of just having qrc files which I tell cmake about.
Here is a minimal project which shows the same issue for me in both QtCreator 4.14 and now 4.15 with Qt 5.15.
CMakeLists.txt replacing project.pro
qml.qrc
main.cpp
main.qml
components/components.qrc
components/CustomLabel.qmlQT += quick SOURCES += main.cpp RESOURCES += qml.qrc components/components.qrc TARGET = qml_testing
CMakeLists.txt :
cmake_minimum_required(VERSION 3.16) project(qml_testing LANGUAGES CXX) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTORCC ON) find_package(Qt5 COMPONENTS Core Gui Quick REQUIRED) add_executable(${PROJECT_NAME} main.cpp qml.qrc components/components.qrc) target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Gui Qt5::Quick)
qml.qrc :
<RCC> <qresource prefix="/"> <file>main.qml</file> </qresource> </RCC>
main.cpp :
#include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char* argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return QGuiApplication::exec(); }
main.qml :
import QtQuick 2.15 import QtQuick.Controls 2.15 //import "components" ApplicationWindow { id: root width: 360 height: 640 visible: true CustomLabel { text: "Label Text" } }
components/components.qrc :
<RCC> <qresource prefix="/"> <file>CustomLabel.qml</file> </qresource> </RCC>
components/CustomLabel.qml :
import QtQuick 2.15 import QtQuick.Controls 2.15 Label { id: root width: 220 font.pixelSize: 13 }
-
@Paul-Hollensen You might also need to set QML_IMPORT_PATH like
set(QML_IMPORT_PATH ${PROJECT_SOURCE_DIR}/components CACHE STRING "QML Import Paths")Also in the main.cpp try to initialize the QRC file using
Q_INIT_RESOURCE(components);
-
Thanks for your reply, Mammamia.
Neither setting QML_IMPORT_PATH nor adding Q_INIT_RESOURCE resolved the issue.
My understanding is QML_IMPORT_PATH is for modules, which I am not building, so qmake or QtCreator must be setting something else. -
@Paul-Hollensen
Ok, I found it.In your components.qrc change the prefix from "/" to "components"
<RCC> <qresource prefix="/components"> <file>CustomLabel.qml</file> </qresource> </RCC>
-
Thank you again, that did it, and has also given me a better sense of how this works.
I'd still like to know if it is possible for QtCreator to find such components without an import using cmake, as it does with qmake.
But I can move forward. Marking solved. Grazie!