Cmakelists.txt file for the module
# File: import/qml_module/ButtonsModule/CMakeLists.txt
# Define the static library for the QML module
qt_add_library(buttons_module STATIC)
find_package(Qt6 REQUIRED COMPONENTS Quick QuickControls2)
include_directories(${Qt${QT_VERSION_MAJOR}Quick_PRIVATE_INCLUDE_DIRS})
if(WIN32)
set(QUICK_CONTROLS "QtQuick.Controls.Windows")
elseif(APPLE)
set(QUICK_CONTROLS "QtQuick.Controls.macOS")
elseif(ANDROID)
set(QUICK_CONTROLS "QtQuick.Controls.Material")
else()
set(QUICK_CONTROLS "QtQuick.Controls.Basic")
endif()
set_target_properties(buttons_module PROPERTIES AUTOMOC ON)
target_link_libraries(buttons_module PRIVATE Qt6::Quick Qt6::QuickPrivate
Qt6::QuickControls2)
list(APPEND MODULE_QML_FILES ButtonBase.qml)
# Define the QML module
qt_add_qml_module(
buttons_module
URI
ButtonsModule
VERSION
1.0
NO_PLUGIN
RESOURCE_PREFIX
/
IMPORTS
QtQuick
${QUICK_CONTROLS}
ENABLE_TYPE_COMPILER
QML_FILES
${MODULE_QML_FILES})
ButtonBase.qml
import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Material
Button {
id: _button
}
Errors
importqml_moduleButtonsModuleButtonBase.qml:2: error: Warnings occurred while importing module “QtQuick.Controls”: [import]
importqml_moduleButtonsModuleButtonBase.qml:5: error: Can’t compile the QML base type “Button” to C++ because it lives in “QtQuick.Controls.Material” instead of the current file’s “ButtonsModule” QML module. [compiler]
What am I doing wrong? Must be something in CmakeLists.txt file or in QML file imports, right?
Tried adding what you can see in my CmakeLists.txt file. Tried adding import QtQuick.Controls.Material
on top of or instead of import QtQuick.Controls
in the QML file
It runs so long as ENABLE_TYPE_COMPILER isn’t in qt_add_qml_module but I want it to precompiled into C++ so I want ENABLE_TYPE_COMPILER to be present.
Using qmltc
https://doc.qt.io/qt-6/qtqml-qml-type-compiler.html
According to some post online in 2022, “Unfortunately, qmltc cannot compile Qt Quick Controls 2 at the moment”. https://www.marketscreener.com/quote/stock/QT-GROUP-OYJ-30049777/news/QML-Type-Compilation-that-new-Qt-Quick-Compiler-part-you-have-not-heard-much-about-39656817/. I’m assuming that in 2024 this is possible though
Kory is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.