Singleton and QML in QT 6.3
-
I am at my wits end with this. I've searched, found "solutions" but nothing works.
I am running Qt 6.3.2 and Qt Creator 8The application builds with no errors, starts but the window does not display. If I hardcode the color it runs.
In a course I took they simply setup the qml.dir file and then imported the styles directory into the myqml module but the course was for Qt 5.
Any help is appreciated .
Thank you.
I have added this to the CMakeLists.txt file with no luck.
# Set QML singleton set_source_files_properties(Style.qml PROPERTIES QT_SINGLETON_TYPE TRUE)I have set qmldir as below and then imported using the import statement shown below.
I have the followingsingleton Style 1.0 Style.qmlmain.qml
style (directory)
Style.qml
qmldir
ui (directory)
myqml.qmlStyle.qml is as singleton:
pragma Singleton import QtQuick QtObject { id: root // Global Items property string theme: "AML" // Main menu items property int menuWidth: (rootWindow.width / 4) property int menuHeight: (rootWindow.height) property string menubgColor: "orange" property string menuItemColor: "pink" } in my myqml.qml I have the following. If I set the rectangle color to a string such as "orange" everything works fine but if I try and use Style.menubgColor for the color the app builds, starts (the exe is in the task manager list) but never displays anything.import QtQuick
import "../styles"Item {
id: rootproperty string currentItemName: mainMenuList.currentItemName signal menuItemClicked(var name) Rectangle { id: mainmenuColorFill width: parent.width height: rootWindow.height color: Style.menubgColor // "orange" MainMenuList { id:mainMenuList anchors.centerIn: parent } } // End Rectangle mainMenuColorFill} // End Item root
@RangerQT said in Singleton and QML in QT 6.3:
I have set qmldir as below
Since you are using Qt 6.3, don't create qmldir files manually. Use
qt_add_qml_module()instead. See https://www.qt.io/blog/introduction-to-the-qml-cmake-apiimport "../styles"This syntax is for importing a folder from disk. But you don't have a QML folder deployed with your application -- the Qt 6 CMake API is designed to build your QML code into a library (which is statically linked to your application, ideally)
Set a URI in
qt_add_qml_module()and import that URI instead. -
Hmm. I'm trying to get my head around this <G>. The last time I used Qt was Qt 5 with pro files. I will read that article - again for the nth time!
You mention static linking so I assume Qt 6 CMake does that for me and I don't have to tell it to do a static library.
Thank you.1). Do I understand we do not need import statements anymore in QML since the qt_add_qml_module should pull them all in so I can access properties, etc without the import statement in my qml files. When would I have a QML folder deployed. I guess I thought that is what the ui and styles were - folders to import but from what you say the CMakeLists.txt should handle that.
So I do import FlightLog which is my URI (no quotes around FlightLog) and that should work. Yes, that works so I just do FlightLog which is my URI instead of the import "somedir".2). Do I need a CMakeLists.txt in each subdirectory or is the one CMakeLists.txt at the project level okay. I assume not but I see examples with CMakeLIsts.txt in subdirectories but then they have cpp and h files in them.
3). I have this in CMakeLists.txt so should this be okay to include all my *.qml files? I am assuming it's okay to do something like ui/AppWindow.qml and styles/Style.qml in the QML_FILES list.
qt_add_executable(FlightLogbook main.cpp ) # QML and other files qt_add_qml_module(FlightLogbook URI FlightLog VERSION 1.0 QML_FILES main.qml ui/AppWindow.qml ui/MainMenu.qml ui/MainMenuList.qml styles/Style.qml SOURCES import/import.h import/import.cpp )4). Do I need the set_source_files_properties(Style.qml PROPERTIES QT_SINGLETON_TYPE TRUE) in my CMakeLists.txt?
5.) I can do color: "orange" in the Rectangle but if I try color: Style.menubgColor the app builds, starts, but no window comes up. Style is recognized and when I hit the . it gives me the properties in the file so for some reason it must not like how I am using it to color the rectangle. This is with and without the pragma statement.
-
A followup to this. First I like the cmake process a lot and how things are handled in Qt6 vs Qt5 so far.
I still can not get a property defined in one qml file used in another. I get a unable to assign [undefined] to QColor. However, if I define the property in this file.
// Qt Imports import QtQuick // Our Imports import FlightLog Item { id: root property string currentItemName: mainMenuList.currentItemName signal menuItemClicked(var name) Rectangle { id: mainmenuColorFill width: parent.width height: rootWindow.height color: CustomStyles.menubgColor // "orange" MainMenuList { id:mainMenuList anchors.centerIn: parent } } // End Rectangle mainMenuColorFill } // End Item rootThe CustomStyles.qml file is
// Qt Imports import QtQuick // Our Imports //import FlightLog QtObject { id: customStyles property string menubgColor: "purple" }However, if I define a property string testcolor: "purple" in main.qml I can use mainqlmwindowid.testcolor and it works.
I did try customStyles.menubgcolor in the MainMenu.qml file and it fails also.
Given it works with the definition in main.qml I assume it means the build did not include the CustomStyles.qml.
-
Hi @RangerQT , did you find a solution to your question? I am fighting with something similar (but using qmake, not cmake yet)
@thomaso said in Singleton and QML in QT 6.3:
Hi @RangerQT , did you find a solution to your question? I am fighting with something similar (but using qmake, not cmake yet)
I never did and stopped pursuing that. Right now I'm working on getting other parts of my application working and then I'll revisit Singletons since I do need them.
-
A followup to this. First I like the cmake process a lot and how things are handled in Qt6 vs Qt5 so far.
I still can not get a property defined in one qml file used in another. I get a unable to assign [undefined] to QColor. However, if I define the property in this file.
// Qt Imports import QtQuick // Our Imports import FlightLog Item { id: root property string currentItemName: mainMenuList.currentItemName signal menuItemClicked(var name) Rectangle { id: mainmenuColorFill width: parent.width height: rootWindow.height color: CustomStyles.menubgColor // "orange" MainMenuList { id:mainMenuList anchors.centerIn: parent } } // End Rectangle mainMenuColorFill } // End Item rootThe CustomStyles.qml file is
// Qt Imports import QtQuick // Our Imports //import FlightLog QtObject { id: customStyles property string menubgColor: "purple" }However, if I define a property string testcolor: "purple" in main.qml I can use mainqlmwindowid.testcolor and it works.
I did try customStyles.menubgcolor in the MainMenu.qml file and it fails also.
Given it works with the definition in main.qml I assume it means the build did not include the CustomStyles.qml.
Is
set_source_files_properties(styles/Style.qml PROPERTIES QT_SINGLETON_TYPE TRUE)before theqt_add_qml_moduleline in your CMakeLists.txt?Also in your
set_source_files_propertiescall you posted above you seem to be omitting thestyles/prefix from the file name which may also be the issue .I suspect the generated qmldir is not marking your Style.qml file as a singleton which can be easily checked by inspecting the generated file. I also encountered this issue and for me it was caused by incorrect ordering in the CMakeLists.txt file like I mentioned above.
-
Is
set_source_files_properties(styles/Style.qml PROPERTIES QT_SINGLETON_TYPE TRUE)before theqt_add_qml_moduleline in your CMakeLists.txt?Also in your
set_source_files_propertiescall you posted above you seem to be omitting thestyles/prefix from the file name which may also be the issue .I suspect the generated qmldir is not marking your Style.qml file as a singleton which can be easily checked by inspecting the generated file. I also encountered this issue and for me it was caused by incorrect ordering in the CMakeLists.txt file like I mentioned above.
@blues-breaker
Thanks for that. I'll give it a try. -
I've been looking for a solution for a long time, but finally figured it out: you have a typo in your source file property name, it is
QT_QML_SINGLETON_TYPE, notQT_SINGLETON_TYPE. If you addset_source_files_properties(Style.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)Before
qt_add_qml_modules, the singleton works without any manualqmldiror c++ code -
I've been looking for a solution for a long time, but finally figured it out: you have a typo in your source file property name, it is
QT_QML_SINGLETON_TYPE, notQT_SINGLETON_TYPE. If you addset_source_files_properties(Style.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)Before
qt_add_qml_modules, the singleton works without any manualqmldiror c++ code -
@IAlexI
Arrgh. So simple! And where to put it is another part of the puzzle. Thank you all.Hi all.
Just to help out anyone else who get into singleton issues, I also found out that having singletons in a sub-directory doesn't work using
set_source_files_properties.Please take a look at this thread.
-
Hi all.
Just to help out anyone else who get into singleton issues, I also found out that having singletons in a sub-directory doesn't work using
set_source_files_properties.Please take a look at this thread.
@DrSnowLabs @IAlexI
I am also running into this issue.I have several custom component in a separate directly which is like a common library for example myTextBox.qml , myTextFiled.qml
and i have a Style.qml file at the same location.
I have added Style.qml in CMAKE:
This sets up the singleton module Style to be used across the application
set_source_files_properties(Style.qml PROPERTIES
QT_QML_SINGLETON_TYPE TRUE
)but still I can' access Style.properties inside the same directory or from another library which uses these common custom components.
any ideas to resolve this issue?
-
Hi all.
Just to help out anyone else who get into singleton issues, I also found out that having singletons in a sub-directory doesn't work using
set_source_files_properties.Please take a look at this thread.
@DrSnowLabs also take a look at https://www.qt.io/blog/implicit-imports-vs.-qml-modules-in-qt-6