Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to create QML singleton
Forum Updated to NodeBB v4.3 + New Features

How to create QML singleton

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
17 Posts 5 Posters 3.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K KejPi

    I do not know how to attach file, so this is cope and paste of minimal example:

    CMakeList.txt

    cmake_minimum_required(VERSION 3.16)
    
    project(SingletonTest VERSION 0.1 LANGUAGES CXX)
    
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(Qt6 6.4 REQUIRED COMPONENTS Quick)
    
    qt_standard_project_setup()
    
    qt_add_executable(appSingletonTest
        main.cpp
    )
    
    set_source_files_properties(qml/Colors.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
    qt_add_qml_module(appSingletonTest
        URI SingletonTest
        VERSION 1.0
        QML_FILES qml/main.qml
        QML_FILES qml/Colors.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(appSingletonTest PROPERTIES
    #    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appSingletonTest
        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(appSingletonTest
        PRIVATE Qt6::Quick
    )
    
    include(GNUInstallDirs)
    install(TARGETS appSingletonTest
        BUNDLE DESTINATION .
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    )
    
    

    main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        const QUrl url(u"qrc:/SingletonTest/qml/main.qml"_qs);
        QObject::connect(
            &engine,
            &QQmlApplicationEngine::objectCreationFailed,
            &app,
            []() { QCoreApplication::exit(-1); },
            Qt::QueuedConnection);
        engine.load(url);
    
        return app.exec();
    }
    

    qml/main.qml:

    import QtQuick
    import SingletonTest
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        Rectangle {
            width: parent.width/2
            height: parent.height/2
            anchors.centerIn: parent
            color: Colors.myColor
        }
    }
    

    qml/Colors.qml:

    pragma Singleton
    import QtQuick
    
    QtObject {
        id: colors
        readonly property color myColor: "red"
    }
    

    When I run it I get this message:

    qrc:/SingletonTest/qml/main.qml:14: TypeError: Colors was a singleton at compile time, but is not a singleton anymore.
    
    GrecKoG Offline
    GrecKoG Offline
    GrecKo
    Qt Champions 2018
    wrote on last edited by
    #5

    What if you put the QML files in the root folder of the project? Colors.qml instead of qml/Colors.qml

    Also I'd remove the second QML_FILES in qt_add_qml_module

    K 1 Reply Last reply
    0
    • GrecKoG GrecKo

      What if you put the QML files in the root folder of the project? Colors.qml instead of qml/Colors.qml

      Also I'd remove the second QML_FILES in qt_add_qml_module

      K Offline
      K Offline
      KejPi
      wrote on last edited by
      #6

      If I move Colors.qml to root folder and change CMakeList.txt accordingly:

      set_source_files_properties(Colors.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
      qt_add_qml_module(appSingletonTest
          URI SingletonTest
          VERSION 1.0
          QML_FILES qml/main.qml
                    Colors.qml
      )
      

      I get this error when I run the application:

      qrc:/SingletonTest/qml/main.qml:14: ReferenceError: Colors is not defined
      
      K 1 Reply Last reply
      0
      • K KejPi

        If I move Colors.qml to root folder and change CMakeList.txt accordingly:

        set_source_files_properties(Colors.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
        qt_add_qml_module(appSingletonTest
            URI SingletonTest
            VERSION 1.0
            QML_FILES qml/main.qml
                      Colors.qml
        )
        

        I get this error when I run the application:

        qrc:/SingletonTest/qml/main.qml:14: ReferenceError: Colors is not defined
        
        K Offline
        K Offline
        KejPi
        wrote on last edited by KejPi
        #7

        It seems all QML files must be in root folder including some other custom components files.

        If I do this, it works:

        set_source_files_properties(Colors.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
        qt_add_qml_module(appSingletonTest
            URI SingletonTest
            VERSION 1.0
            QML_FILES main.qml
                      Colors.qml
                      SomeComponent.qml
        )
        

        This strange, so far I put all my QML files to dedicated folder (qml/) to separate it from C++ and everything was working (see my first example code) - only the singleton causes some problem.

        I could consider this as workaround but I do not like it since I will mix QML with C++ and it will be a mess :-(

        EDIT: It seems that this construction works too:

        set_source_files_properties(qmlfiles/Colors.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
        set_source_files_properties(qmlfiles/main.qml PROPERTIES QT_RESOURCE_ALIAS main.qml)
        set_source_files_properties(qmlfiles/Colors.qml PROPERTIES QT_RESOURCE_ALIAS Colors.qml)
        set_source_files_properties(qmlfiles/SomeComponent.qml PROPERTIES QT_RESOURCE_ALIAS SomeComponent.qml)
        qt_add_qml_module(appSingletonTest
            URI SingletonTest
            VERSION 1.0
            QML_FILES qmlfiles/main.qml
                      qmlfiles/Colors.qml
                      qmlfiles/SomeComponent.qml
        )
        
        ekkescornerE J 2 Replies Last reply
        0
        • K KejPi

          It seems all QML files must be in root folder including some other custom components files.

          If I do this, it works:

          set_source_files_properties(Colors.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
          qt_add_qml_module(appSingletonTest
              URI SingletonTest
              VERSION 1.0
              QML_FILES main.qml
                        Colors.qml
                        SomeComponent.qml
          )
          

          This strange, so far I put all my QML files to dedicated folder (qml/) to separate it from C++ and everything was working (see my first example code) - only the singleton causes some problem.

          I could consider this as workaround but I do not like it since I will mix QML with C++ and it will be a mess :-(

          EDIT: It seems that this construction works too:

          set_source_files_properties(qmlfiles/Colors.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
          set_source_files_properties(qmlfiles/main.qml PROPERTIES QT_RESOURCE_ALIAS main.qml)
          set_source_files_properties(qmlfiles/Colors.qml PROPERTIES QT_RESOURCE_ALIAS Colors.qml)
          set_source_files_properties(qmlfiles/SomeComponent.qml PROPERTIES QT_RESOURCE_ALIAS SomeComponent.qml)
          qt_add_qml_module(appSingletonTest
              URI SingletonTest
              VERSION 1.0
              QML_FILES qmlfiles/main.qml
                        qmlfiles/Colors.qml
                        qmlfiles/SomeComponent.qml
          )
          
          ekkescornerE Offline
          ekkescornerE Offline
          ekkescorner
          Qt Champions 2016
          wrote on last edited by
          #8

          @KejPi open a bugreport because QML SINGLETON doesn't work if not in root folder

          ekke ... Qt Champion 2016 | 2024 ... mobile business apps
          5.15 --> 6.9 https://t1p.de/ekkeChecklist
          QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

          MesrineM 1 Reply Last reply
          0
          • ekkescornerE ekkescorner

            @KejPi open a bugreport because QML SINGLETON doesn't work if not in root folder

            MesrineM Offline
            MesrineM Offline
            Mesrine
            wrote on last edited by
            #9

            @ekkescorner

            I do not know why the one from the post does not work, but singletons from subdirectories work.

            I used here

            ekkescornerE K 2 Replies Last reply
            0
            • MesrineM Mesrine

              @ekkescorner

              I do not know why the one from the post does not work, but singletons from subdirectories work.

              I used here

              ekkescornerE Offline
              ekkescornerE Offline
              ekkescorner
              Qt Champions 2016
              wrote on last edited by
              #10

              @Mesrine aaah - good to know. just porting my apps from QMake to CMake. next step will be to modernize my apps, where I'll need QML SINGLETONS, too

              ekke ... Qt Champion 2016 | 2024 ... mobile business apps
              5.15 --> 6.9 https://t1p.de/ekkeChecklist
              QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

              1 Reply Last reply
              0
              • K KejPi

                It seems all QML files must be in root folder including some other custom components files.

                If I do this, it works:

                set_source_files_properties(Colors.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
                qt_add_qml_module(appSingletonTest
                    URI SingletonTest
                    VERSION 1.0
                    QML_FILES main.qml
                              Colors.qml
                              SomeComponent.qml
                )
                

                This strange, so far I put all my QML files to dedicated folder (qml/) to separate it from C++ and everything was working (see my first example code) - only the singleton causes some problem.

                I could consider this as workaround but I do not like it since I will mix QML with C++ and it will be a mess :-(

                EDIT: It seems that this construction works too:

                set_source_files_properties(qmlfiles/Colors.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
                set_source_files_properties(qmlfiles/main.qml PROPERTIES QT_RESOURCE_ALIAS main.qml)
                set_source_files_properties(qmlfiles/Colors.qml PROPERTIES QT_RESOURCE_ALIAS Colors.qml)
                set_source_files_properties(qmlfiles/SomeComponent.qml PROPERTIES QT_RESOURCE_ALIAS SomeComponent.qml)
                qt_add_qml_module(appSingletonTest
                    URI SingletonTest
                    VERSION 1.0
                    QML_FILES qmlfiles/main.qml
                              qmlfiles/Colors.qml
                              qmlfiles/SomeComponent.qml
                )
                
                J Offline
                J Offline
                Janson
                wrote on last edited by Janson
                #11
                This post is deleted!
                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  KejPi
                  wrote on last edited by
                  #12

                  I have a small example project that clearly shows it does not work as expected. I can share it but I do not know how to do it here. Maybe I did something wrong - I have juste created project in QtCreator and moved all QML files in subdirectory.

                  1 Reply Last reply
                  0
                  • MesrineM Mesrine

                    @ekkescorner

                    I do not know why the one from the post does not work, but singletons from subdirectories work.

                    I used here

                    K Offline
                    K Offline
                    KejPi
                    wrote on last edited by
                    #13

                    @Mesrine Could it be that you only have singleton in the module while I have all my QML files there?

                    1 Reply Last reply
                    0
                    • K KejPi

                      I do not know how to attach file, so this is cope and paste of minimal example:

                      CMakeList.txt

                      cmake_minimum_required(VERSION 3.16)
                      
                      project(SingletonTest VERSION 0.1 LANGUAGES CXX)
                      
                      set(CMAKE_CXX_STANDARD_REQUIRED ON)
                      
                      find_package(Qt6 6.4 REQUIRED COMPONENTS Quick)
                      
                      qt_standard_project_setup()
                      
                      qt_add_executable(appSingletonTest
                          main.cpp
                      )
                      
                      set_source_files_properties(qml/Colors.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
                      qt_add_qml_module(appSingletonTest
                          URI SingletonTest
                          VERSION 1.0
                          QML_FILES qml/main.qml
                          QML_FILES qml/Colors.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(appSingletonTest PROPERTIES
                      #    MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appSingletonTest
                          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(appSingletonTest
                          PRIVATE Qt6::Quick
                      )
                      
                      include(GNUInstallDirs)
                      install(TARGETS appSingletonTest
                          BUNDLE DESTINATION .
                          LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
                          RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
                      )
                      
                      

                      main.cpp:

                      #include <QGuiApplication>
                      #include <QQmlApplicationEngine>
                      
                      int main(int argc, char *argv[])
                      {
                          QGuiApplication app(argc, argv);
                      
                          QQmlApplicationEngine engine;
                          const QUrl url(u"qrc:/SingletonTest/qml/main.qml"_qs);
                          QObject::connect(
                              &engine,
                              &QQmlApplicationEngine::objectCreationFailed,
                              &app,
                              []() { QCoreApplication::exit(-1); },
                              Qt::QueuedConnection);
                          engine.load(url);
                      
                          return app.exec();
                      }
                      

                      qml/main.qml:

                      import QtQuick
                      import SingletonTest
                      
                      Window {
                          width: 640
                          height: 480
                          visible: true
                          title: qsTr("Hello World")
                      
                          Rectangle {
                              width: parent.width/2
                              height: parent.height/2
                              anchors.centerIn: parent
                              color: Colors.myColor
                          }
                      }
                      

                      qml/Colors.qml:

                      pragma Singleton
                      import QtQuick
                      
                      QtObject {
                          id: colors
                          readonly property color myColor: "red"
                      }
                      

                      When I run it I get this message:

                      qrc:/SingletonTest/qml/main.qml:14: TypeError: Colors was a singleton at compile time, but is not a singleton anymore.
                      
                      MesrineM Offline
                      MesrineM Offline
                      Mesrine
                      wrote on last edited by Mesrine
                      #14

                      @KejPi

                      I have used your minimal example and works ok :) .

                      The only thing I changed was that instead of CMakeList.txt i named CMakeLists.txt.

                      I have tested it with qt(shared libraries) 6.5,6.6,6.7 in Linux.

                      Make sure you are using the correct CMakeLists.txt.
                      And let us know your platform and Qt version.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        KejPi
                        wrote on last edited by KejPi
                        #15

                        Yes, it was only typo, I use the CMakeLists.txt created by Qt Creator.
                        I am running it on MacOS 14.2, Qt version 6.5.3

                        MesrineM 1 Reply Last reply
                        0
                        • K KejPi

                          Yes, it was only typo, I use the CMakeLists.txt created by Qt Creator.
                          I am running it on MacOS 14.2, Qt version 6.5.3

                          MesrineM Offline
                          MesrineM Offline
                          Mesrine
                          wrote on last edited by
                          #16

                          @KejPi
                          Do you run the application that is on the build directory or the installed one?

                          Try putting
                          set_source_files_properties(qml/Colors.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)

                          before

                          qt_add_executable(appSingletonTest
                              main.cpp
                          )
                          

                          in your minimal example.
                          If that does not work try removing some macos properties in the CMakeLists.txt.
                          If nothing works I would say you can open a bug as suggested by @ekkescorner

                          K 1 Reply Last reply
                          0
                          • MesrineM Mesrine

                            @KejPi
                            Do you run the application that is on the build directory or the installed one?

                            Try putting
                            set_source_files_properties(qml/Colors.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)

                            before

                            qt_add_executable(appSingletonTest
                                main.cpp
                            )
                            

                            in your minimal example.
                            If that does not work try removing some macos properties in the CMakeLists.txt.
                            If nothing works I would say you can open a bug as suggested by @ekkescorner

                            K Offline
                            K Offline
                            KejPi
                            wrote on last edited by KejPi
                            #17

                            @Mesrine I run the application directly from Qt Creator after building it.
                            Moving set_source_files_properties did not help, the problem is the same.

                            Bug report created https://bugreports.qt.io/browse/QTBUG-121758

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved