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. Singleton and QML in QT 6.3
Qt 6.11 is out! See what's new in the release blog

Singleton and QML in QT 6.3

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
13 Posts 8 Posters 4.9k Views 3 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.
  • R Offline
    R Offline
    RangerQT
    wrote on last edited by RangerQT
    #3

    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.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      RangerQT
      wrote on last edited by RangerQT
      #4

      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 root
      

      The 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.

      B 1 Reply Last reply
      0
      • T Offline
        T Offline
        thomaso
        wrote on last edited by
        #5

        Hi @RangerQT , did you find a solution to your question? I am fighting with something similar (but using qmake, not cmake yet)

        R 1 Reply Last reply
        0
        • T thomaso

          Hi @RangerQT , did you find a solution to your question? I am fighting with something similar (but using qmake, not cmake yet)

          R Offline
          R Offline
          RangerQT
          wrote on last edited by
          #6

          @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.

          1 Reply Last reply
          0
          • R RangerQT

            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 root
            

            The 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.

            B Offline
            B Offline
            blues-breaker
            wrote on last edited by
            #7

            Is set_source_files_properties(styles/Style.qml PROPERTIES QT_SINGLETON_TYPE TRUE) before the qt_add_qml_module line in your CMakeLists.txt?

            Also in your set_source_files_properties call you posted above you seem to be omitting the styles/ 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.

            R 1 Reply Last reply
            0
            • B blues-breaker

              Is set_source_files_properties(styles/Style.qml PROPERTIES QT_SINGLETON_TYPE TRUE) before the qt_add_qml_module line in your CMakeLists.txt?

              Also in your set_source_files_properties call you posted above you seem to be omitting the styles/ 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.

              R Offline
              R Offline
              RangerQT
              wrote on last edited by
              #8

              @blues-breaker
              Thanks for that. I'll give it a try.

              1 Reply Last reply
              0
              • I Offline
                I Offline
                IAlexI
                wrote on last edited by IAlexI
                #9

                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, not QT_SINGLETON_TYPE . If you add

                set_source_files_properties(Style.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
                

                Before qt_add_qml_modules, the singleton works without any manual qmldir or c++ code

                R 1 Reply Last reply
                1
                • I IAlexI

                  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, not QT_SINGLETON_TYPE . If you add

                  set_source_files_properties(Style.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
                  

                  Before qt_add_qml_modules, the singleton works without any manual qmldir or c++ code

                  R Offline
                  R Offline
                  RangerQT
                  wrote on last edited by
                  #10

                  @IAlexI
                  Arrgh. So simple! And where to put it is another part of the puzzle. Thank you all.

                  D 1 Reply Last reply
                  0
                  • R RangerQT

                    @IAlexI
                    Arrgh. So simple! And where to put it is another part of the puzzle. Thank you all.

                    D Offline
                    D Offline
                    DrSnowLabs
                    wrote on last edited by
                    #11

                    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.

                    K ekkescornerE 2 Replies Last reply
                    1
                    • D DrSnowLabs

                      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.

                      K Offline
                      K Offline
                      kaushikv
                      wrote on last edited by
                      #12

                      @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?

                      1 Reply Last reply
                      0
                      • D DrSnowLabs

                        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.

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

                        @DrSnowLabs also take a look at https://www.qt.io/blog/implicit-imports-vs.-qml-modules-in-qt-6

                        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

                        • Login

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