qt_add_qml_module + singleton confusion
-
TL;DR:
- Do I need
qmldir
or will CMake generate it? - Do I need
RESOURCE_PREFIX /
inqt_add_qml_module()
? - Do I need to link against something after calling
qt_add_qml_module()
? - When do I call
qt_add_qml_module()
? Before or afterqt_add_resources()
? Before or afterqt_add_executable()
? - What is the correct way of importing?
import "FontAwesome"
import FontAwesome
import FontAwesome 1.0
The issue
I have a singleton QML file I am trying to add, it looks like:
pragma Singleton import QtQuick Object { FontLoader { id: regular source: "./fa-regular-400.otf" } property string fontFamily: regular.name property string addressBook : "\uf2b9" }
So that I can do:
import FontAwesome Text { text: FontAwesome.addressBook }
I checked the documentation on qt_add_qml_module but unfortunately it seems to be written by a programmer, thus incomprehensible to regular humans.
I tried:
set_source_files_properties(FontAwesome.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE) qt_add_qml_module(FontAwesome URI FontAwesome VERSION 1.0 QML_FILES FontAwesome.qml )
But when running the application I get:
[ 0.268 warning] test default unknown - qrc:/app/qml/Main.qml:11:1: module "FontAwesome" is not installed import FontAwesome ^
Note: I can specify a filename that does not exist and it continues the configure step regardless. So I don't know if
qt_add_qml_module()
actually does anything...Lets try to link against it because why not:
target_link_libraries(test PRIVATE Qt6::Core Qt6::Gui [...] FontAwesomeplugin )
But that yields in:
CMake Error at src/CMakeLists.txt:34 (target_link_libraries): Target "FontAwesomeplugin" of type MODULE_LIBRARY may not be linked into another target. One may link only to INTERFACE, OBJECT, STATIC or SHARED libraries, or to executables with the ENABLE_EXPORTS property set.
How to proceed? Anyone have some tips?
- Do I need
-
Any luck?
Am having similar issues.
Am following this
https://wiki.qt.io/Qml_Styling#Approach_2:_Style_Singleton
and this
https://doc.qt.io/qt-6/qtqml-modules-qmldir.html#object-type-declaration
but those still leave the reader hanging re:
the CMake side and
if/why so much boilerplate is really needed (in the .qml and qmldir and CMake) just to make one style property accessible from other qml files.
Thank you. -
@RobertB said in qt_add_qml_module + singleton confusion:
FontAwesome
I believe what you are doing is mostly correct.
It would help if you linked to theFontAwesome
target.
You link to theFontAwesomeplugin
target if theFontAwsome
library is static.
Sometimes the engine would not find your moduleFontAwsome
when is compiled as a shared library for that the module has to be loaded at runtime by setting theQML_IMPORT_PATH
environment variable or as explained here.To be safer try to not use the same name for the Singleton type and the module.
-
The following works for me when running the app, however when trying to preview it under the Design view in Qt Creator, it doesn't display the expected color.
// CMakeLists.txt
...
qt_add_qml_module(appsingleton
URI singleton
VERSION 1.0
QML_FILES Main.qml
QML_FILES Shapes.qml
)qt_add_qml_module(CustomStyles
URI CustomStyles
VERSION 1.0
QML_FILES CustomStyles/Style.qml
RESOURCES CustomStyles/qmldir
)
..// CustomStyles/Style.qml
pragma Singleton
import QtQuick 2.15QtObject {
property color color: "green"
}// CustomStyles/qmldir
module CustomStyles
singleton Style 1.0 Style.qml// Shapes.qml
import CustomStyles
...
color: Style.color -
@RobertB
If you do
File | New Project | Qt Quick Application
x Creates a project that you can open in Qt Design Studio
that gives you a "working" starter project which uses a singleton Constants.qml
Well it works running, but also doesn't seem to work in Design mode of Qt Creator?