Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. issues with using qt_policy(SET QTP0001 NEW)
Forum Updated to NodeBB v4.3 + New Features

issues with using qt_policy(SET QTP0001 NEW)

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 2 Posters 4.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.
  • mzimmersM mzimmers

    @JKSH I add function.js as follows:

    qt_add_qml_module(appnga_demo
        URI nga_demo
        VERSION 1.0
        QML_FILES
        main.qml
        functions.js
        Activitydrawer.qml
        ...
    

    I put the code you suggested in my main.cpp, and got a ton of output, but this line was included:

    ":/qt/qml/nga_demo/functions.js"
    

    EDIT: whatever is wrong with my attempted use of Functions.js (now renamed with an uppercase first letter) is also wrong with my Colors singleton, registered accordingly:

    qmlRegisterSingletonType( QUrl( "qrc:/Colors.qml" ), "colors.stylesheet", 1, 0, "Colors" );
    

    and starting with:

    pragma Singleton
    
    import QtQuick 2.0
    import QtQml
    
    QtObject {
        property color callToAction: "#6845da"
        property color accent: "#4147f0"
        property color accentIllust: "#e3a26b"
    

    There is a section at the end of this page that discusses singletons:
    The API so far was used to create new objects, and thus it’s unsuitable for QML singletons.
    if I understand this correctly, I guess I need to reimplement all of my singletons as well?

    JKSHJ Offline
    JKSHJ Offline
    JKSH
    Moderators
    wrote on last edited by
    #6

    @mzimmers said in issues with using qt_policy(SET QTP0001 NEW):

    ":/qt/qml/nga_demo/functions.js"
    

    There we go. You tried to import qrc:/qt/qml/functions.js but you need to import qrc:/qt/qml/nga_demo/functions.js. So adjust your import statement accordingly.

    qmlRegisterSingletonType( QUrl( "qrc:/Colors.qml" ), "colors.stylesheet", 1, 0, "Colors" );
    

    Do not mix qt_add_qml_module() with qmlRegisterSingletonType(). Let CMake do the automatic registration; get rid of qmlRegisterSingletonType()

    There is a section at the end of this page that discusses singletons:
    The API so far was used to create new objects, and thus it’s unsuitable for QML singletons.
    if I understand this correctly, I guess I need to reimplement all of my singletons as well?

    No, that section discusses singletons implemented in C++. Your implementation is fine, you just need

    set_source_files_properties(Colors.qml PROPERTIES
        QT_QML_SINGLETON_TYPE TRUE
    )
    

    See https://doc.qt.io/qt-6/qt-target-qml-sources.html#qt-target-qml-sources-example for an example.

    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

    mzimmersM 1 Reply Last reply
    1
    • JKSHJ JKSH

      @mzimmers said in issues with using qt_policy(SET QTP0001 NEW):

      ":/qt/qml/nga_demo/functions.js"
      

      There we go. You tried to import qrc:/qt/qml/functions.js but you need to import qrc:/qt/qml/nga_demo/functions.js. So adjust your import statement accordingly.

      qmlRegisterSingletonType( QUrl( "qrc:/Colors.qml" ), "colors.stylesheet", 1, 0, "Colors" );
      

      Do not mix qt_add_qml_module() with qmlRegisterSingletonType(). Let CMake do the automatic registration; get rid of qmlRegisterSingletonType()

      There is a section at the end of this page that discusses singletons:
      The API so far was used to create new objects, and thus it’s unsuitable for QML singletons.
      if I understand this correctly, I guess I need to reimplement all of my singletons as well?

      No, that section discusses singletons implemented in C++. Your implementation is fine, you just need

      set_source_files_properties(Colors.qml PROPERTIES
          QT_QML_SINGLETON_TYPE TRUE
      )
      

      See https://doc.qt.io/qt-6/qt-target-qml-sources.html#qt-target-qml-sources-example for an example.

      mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on last edited by
      #7

      @JKSH OK, this is making progress. I added this to my CMakeLists.txt file:

      set_source_files_properties(Color.qml PROPERTIES
          QT_QML_SINGLETON_TYPE TRUE
      )
      

      And removed the qmlRegisterSingletonType() line. Now, this line in my Main.qml:

      import Colors
      

      Gives an error "QML module not found." And Colors.qml is still in my qt_add_qml_module() statement. What might I have missed here?

      Thanks...

      JKSHJ 1 Reply Last reply
      0
      • mzimmersM mzimmers

        @JKSH OK, this is making progress. I added this to my CMakeLists.txt file:

        set_source_files_properties(Color.qml PROPERTIES
            QT_QML_SINGLETON_TYPE TRUE
        )
        

        And removed the qmlRegisterSingletonType() line. Now, this line in my Main.qml:

        import Colors
        

        Gives an error "QML module not found." And Colors.qml is still in my qt_add_qml_module() statement. What might I have missed here?

        Thanks...

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #8

        @mzimmers said in issues with using qt_policy(SET QTP0001 NEW):

        import Colors
        

        You must import the URI as specified in the qt_add_qml_module() function that contains Colors.qml

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        mzimmersM 1 Reply Last reply
        0
        • JKSHJ JKSH

          @mzimmers said in issues with using qt_policy(SET QTP0001 NEW):

          import Colors
          

          You must import the URI as specified in the qt_add_qml_module() function that contains Colors.qml

          mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #9

          @JKSH said in issues with using qt_policy(SET QTP0001 NEW):

          You must import the URI as specified in the qt_add_qml_module() function that contains Colors.qml

          My function reads as:

          qt_add_qml_module(appnga_demo
              URI nga_demo
              VERSION 1.0
              QML_FILES
              (other files)
              Colors.qml
              (other files)
          

          I don't know what you mean by "import the URI" -- do I need to prefix my import statement (I've tried a few combinations, but none worked)?

          import "nga_demo/Colors.qml" // runtime error: no such directory
          import "qrc:/nga_demo/Colors.qml" // same error as above
          import "qrc:/qt/qml/nga_demo/Colors.qml" // same error as above
          

          Thanks for any clarification.

          JKSHJ 1 Reply Last reply
          0
          • mzimmersM mzimmers

            @JKSH said in issues with using qt_policy(SET QTP0001 NEW):

            You must import the URI as specified in the qt_add_qml_module() function that contains Colors.qml

            My function reads as:

            qt_add_qml_module(appnga_demo
                URI nga_demo
                VERSION 1.0
                QML_FILES
                (other files)
                Colors.qml
                (other files)
            

            I don't know what you mean by "import the URI" -- do I need to prefix my import statement (I've tried a few combinations, but none worked)?

            import "nga_demo/Colors.qml" // runtime error: no such directory
            import "qrc:/nga_demo/Colors.qml" // same error as above
            import "qrc:/qt/qml/nga_demo/Colors.qml" // same error as above
            

            Thanks for any clarification.

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by JKSH
            #10

            @mzimmers said in issues with using qt_policy(SET QTP0001 NEW):

            I don't know what you mean by "import the URI"

            qt_add_qml_module(appnga_demo
                URI nga_demo              # <-- This is your URI
            

            So, you do import nga_demo

            import "nga_demo/Colors.qml" // runtime error: no such directory
            import "qrc:/nga_demo/Colors.qml" // same error as above
            import "qrc:/qt/qml/nga_demo/Colors.qml" // same error as above
            

            We import QML modules; we don't import individual QML files (I can see why this is confusing, since you imported a *.js file)

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            mzimmersM 1 Reply Last reply
            1
            • JKSHJ JKSH

              @mzimmers said in issues with using qt_policy(SET QTP0001 NEW):

              I don't know what you mean by "import the URI"

              qt_add_qml_module(appnga_demo
                  URI nga_demo              # <-- This is your URI
              

              So, you do import nga_demo

              import "nga_demo/Colors.qml" // runtime error: no such directory
              import "qrc:/nga_demo/Colors.qml" // same error as above
              import "qrc:/qt/qml/nga_demo/Colors.qml" // same error as above
              

              We import QML modules; we don't import individual QML files (I can see why this is confusing, since you imported a *.js file)

              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #11

              @JKSH oh, I see...I think I'm starting to see the value in this approach.

              So, I replaced all my import references from Color to nga_demo, which got rid of those runtime errors. Now, how do I go about referencing the properties defined in Colors.qml? Lines like this:

                  property color textColor: checked ? Colors.primaryPurple : Colors.primaryText
              

              Don't work any more. Is Colors still a module, or is it now part of a larger module nga_demo?

              Thanks...

              JKSHJ 1 Reply Last reply
              0
              • mzimmersM mzimmers

                @JKSH oh, I see...I think I'm starting to see the value in this approach.

                So, I replaced all my import references from Color to nga_demo, which got rid of those runtime errors. Now, how do I go about referencing the properties defined in Colors.qml? Lines like this:

                    property color textColor: checked ? Colors.primaryPurple : Colors.primaryText
                

                Don't work any more. Is Colors still a module, or is it now part of a larger module nga_demo?

                Thanks...

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #12

                @mzimmers said in issues with using qt_policy(SET QTP0001 NEW):

                @JKSH oh, I see...I think I'm starting to see the value in this approach.

                So, I replaced all my import references from Color to nga_demo, which got rid of those runtime errors.

                Great!

                Now, how do I go about referencing the properties defined in Colors.qml? Lines like this:

                    property color textColor: checked ? Colors.primaryPurple : Colors.primaryText
                

                Don't work any more. Is Colors still a module, or is it now part of a larger module nga_demo?

                It should work. Colors is not a module, it is a QML class/singleton inside the nga_demo module.

                What is the error message?

                Is set_source_files_properties(Color.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE) written before qt_add_qml_module()?

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                mzimmersM JKSHJ 2 Replies Last reply
                0
                • JKSHJ JKSH

                  @mzimmers said in issues with using qt_policy(SET QTP0001 NEW):

                  @JKSH oh, I see...I think I'm starting to see the value in this approach.

                  So, I replaced all my import references from Color to nga_demo, which got rid of those runtime errors.

                  Great!

                  Now, how do I go about referencing the properties defined in Colors.qml? Lines like this:

                      property color textColor: checked ? Colors.primaryPurple : Colors.primaryText
                  

                  Don't work any more. Is Colors still a module, or is it now part of a larger module nga_demo?

                  It should work. Colors is not a module, it is a QML class/singleton inside the nga_demo module.

                  What is the error message?

                  Is set_source_files_properties(Color.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE) written before qt_add_qml_module()?

                  mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on last edited by
                  #13

                  @JKSH currently the set_source_files_properties() call is just after the qt_add_qml_module(), but in a small test project I tried it both ways with the same (bad) results.

                  Just to sanity check, I am no longer using my qml.qrc file, as my CMakeLists.txt file contains:

                  set(CMAKE_AUTOMOC ON)
                  set(CMAKE_AUTORCC ON)
                  

                  This is OK, right?

                  Thanks...

                  1 Reply Last reply
                  0
                  • JKSHJ JKSH

                    @mzimmers said in issues with using qt_policy(SET QTP0001 NEW):

                    @JKSH oh, I see...I think I'm starting to see the value in this approach.

                    So, I replaced all my import references from Color to nga_demo, which got rid of those runtime errors.

                    Great!

                    Now, how do I go about referencing the properties defined in Colors.qml? Lines like this:

                        property color textColor: checked ? Colors.primaryPurple : Colors.primaryText
                    

                    Don't work any more. Is Colors still a module, or is it now part of a larger module nga_demo?

                    It should work. Colors is not a module, it is a QML class/singleton inside the nga_demo module.

                    What is the error message?

                    Is set_source_files_properties(Color.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE) written before qt_add_qml_module()?

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #14

                    @JKSH said in issues with using qt_policy(SET QTP0001 NEW):

                    What is the error message?

                    Please share the message

                    @mzimmers said in issues with using qt_policy(SET QTP0001 NEW):

                    in a small test project

                    Please share your small test project

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    mzimmersM 1 Reply Last reply
                    0
                    • JKSHJ JKSH

                      @JKSH said in issues with using qt_policy(SET QTP0001 NEW):

                      What is the error message?

                      Please share the message

                      @mzimmers said in issues with using qt_policy(SET QTP0001 NEW):

                      in a small test project

                      Please share your small test project

                      mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on last edited by
                      #15

                      @JKSH error message is: Unable to assign [undefined] to QColor.

                      CMakeLists.txt (mostly boilerplate from Creator):

                      cmake_minimum_required(VERSION 3.16)
                      
                      project(newqml VERSION 0.1 LANGUAGES CXX)
                      
                      set(CMAKE_CXX_STANDARD_REQUIRED ON)
                      
                      find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
                      
                      qt_standard_project_setup(REQUIRES 6.5)
                      
                      qt_add_executable(appnewqml
                          main.cpp
                      )
                      qt_add_qml_module(appnewqml
                          URI newqml
                          VERSION 1.0
                          QML_FILES
                              Main.qml
                              Colors.qml
                      )
                      set_source_files_properties(Color.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
                      
                      set_target_properties(appnewqml PROPERTIES
                          MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
                          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(appnewqml
                          PRIVATE Qt6::Quick
                      )
                      
                      install(TARGETS appnewqml
                          BUNDLE DESTINATION .
                          LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
                      

                      main.cpp:

                      #include <QGuiApplication>
                      #include <QQmlApplicationEngine>
                      
                      
                      int main(int argc, char *argv[])
                      {
                          QGuiApplication app(argc, argv);
                      
                          QQmlApplicationEngine engine;
                          QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
                              &app, []() { QCoreApplication::exit(-1); },
                              Qt::QueuedConnection);
                          engine.loadFromModule("newqml", "Main");
                      
                          return app.exec();
                      }
                      

                      Main.qml:

                      import QtQuick
                      import QtQuick.Window
                      import newqml
                      
                      Window {
                          width: 640
                          height: 480
                          visible: true
                          title: qsTr("Hello World")
                          color: Colors.cards
                      }
                      

                      Thanks for looking at this.

                      JKSHJ 1 Reply Last reply
                      0
                      • mzimmersM mzimmers

                        @JKSH error message is: Unable to assign [undefined] to QColor.

                        CMakeLists.txt (mostly boilerplate from Creator):

                        cmake_minimum_required(VERSION 3.16)
                        
                        project(newqml VERSION 0.1 LANGUAGES CXX)
                        
                        set(CMAKE_CXX_STANDARD_REQUIRED ON)
                        
                        find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
                        
                        qt_standard_project_setup(REQUIRES 6.5)
                        
                        qt_add_executable(appnewqml
                            main.cpp
                        )
                        qt_add_qml_module(appnewqml
                            URI newqml
                            VERSION 1.0
                            QML_FILES
                                Main.qml
                                Colors.qml
                        )
                        set_source_files_properties(Color.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
                        
                        set_target_properties(appnewqml PROPERTIES
                            MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
                            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(appnewqml
                            PRIVATE Qt6::Quick
                        )
                        
                        install(TARGETS appnewqml
                            BUNDLE DESTINATION .
                            LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
                        

                        main.cpp:

                        #include <QGuiApplication>
                        #include <QQmlApplicationEngine>
                        
                        
                        int main(int argc, char *argv[])
                        {
                            QGuiApplication app(argc, argv);
                        
                            QQmlApplicationEngine engine;
                            QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
                                &app, []() { QCoreApplication::exit(-1); },
                                Qt::QueuedConnection);
                            engine.loadFromModule("newqml", "Main");
                        
                            return app.exec();
                        }
                        

                        Main.qml:

                        import QtQuick
                        import QtQuick.Window
                        import newqml
                        
                        Window {
                            width: 640
                            height: 480
                            visible: true
                            title: qsTr("Hello World")
                            color: Colors.cards
                        }
                        

                        Thanks for looking at this.

                        JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by JKSH
                        #16

                        @mzimmers said in issues with using qt_policy(SET QTP0001 NEW):

                        qt_add_qml_module(appnewqml
                            URI newqml
                            VERSION 1.0
                            QML_FILES
                                Main.qml
                                Colors.qml
                        )
                        set_source_files_properties(Color.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
                        
                        1. It should be "Colors.qml", not "Color.qml"
                        2. Put set_source_files_properties(...) before qt_add_qml_module()

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        mzimmersM 1 Reply Last reply
                        1
                        • JKSHJ JKSH

                          @mzimmers said in issues with using qt_policy(SET QTP0001 NEW):

                          qt_add_qml_module(appnewqml
                              URI newqml
                              VERSION 1.0
                              QML_FILES
                                  Main.qml
                                  Colors.qml
                          )
                          set_source_files_properties(Color.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
                          
                          1. It should be "Colors.qml", not "Color.qml"
                          2. Put set_source_files_properties(...) before qt_add_qml_module()
                          mzimmersM Offline
                          mzimmersM Offline
                          mzimmers
                          wrote on last edited by mzimmers
                          #17

                          @JKSH oops.

                          OK, definitely making progress here. It works in my minimal program, and I'm no longer getting the "undefined" error in my real program.

                          Several of my qml files are in a subdirectory "custom," which has its own CMakeFiles.txt file:

                          cmake_minimum_required(VERSION 3.16)
                          project(custom)
                          qt_add_qml_module(custom
                              URI custom
                              VERSION 1.0
                              QML_FILES
                              Roundbutton_custom.qml
                              Scrollbar_custom.qml
                              Switch_custom.qml
                              Tabbutton.qml
                              STATIC
                          )
                          

                          My main CMakeLists.txt file has an entry:

                          add_subdirectory(custom)
                          

                          and this all worked fine before I started switching over to 6.5. Do I need to do something extra for the files in this subdirectory? There's nothing special about them (no singletons, etc.)...they're just regular qml files.

                          Thanks...

                          EDIT:

                          I realize this no longer pertains to the original question in this topic; would it be better if I opened a new topic for this?

                          JKSHJ 1 Reply Last reply
                          0
                          • mzimmersM mzimmers

                            @JKSH oops.

                            OK, definitely making progress here. It works in my minimal program, and I'm no longer getting the "undefined" error in my real program.

                            Several of my qml files are in a subdirectory "custom," which has its own CMakeFiles.txt file:

                            cmake_minimum_required(VERSION 3.16)
                            project(custom)
                            qt_add_qml_module(custom
                                URI custom
                                VERSION 1.0
                                QML_FILES
                                Roundbutton_custom.qml
                                Scrollbar_custom.qml
                                Switch_custom.qml
                                Tabbutton.qml
                                STATIC
                            )
                            

                            My main CMakeLists.txt file has an entry:

                            add_subdirectory(custom)
                            

                            and this all worked fine before I started switching over to 6.5. Do I need to do something extra for the files in this subdirectory? There's nothing special about them (no singletons, etc.)...they're just regular qml files.

                            Thanks...

                            EDIT:

                            I realize this no longer pertains to the original question in this topic; would it be better if I opened a new topic for this?

                            JKSHJ Offline
                            JKSHJ Offline
                            JKSH
                            Moderators
                            wrote on last edited by
                            #18

                            @mzimmers said in issues with using qt_policy(SET QTP0001 NEW):

                            OK, definitely making progress here. It works in my minimal program, and I'm no longer getting the "undefined" error in my real program.

                            Congrats!

                            Several of my qml files are in a subdirectory "custom," which has its own CMakeFiles.txt file

                            Notice that this is a different QML module with a different URI.

                            • main.qml needs to do import custom to use Tabbutton
                            • Tabbutton.qml needs to do import nga_demo to use Colors

                            realize this no longer pertains to the original question in this topic; would it be better if I opened a new topic for this?

                            Yes, best open new threads for new questions :)

                            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                            1 Reply Last reply
                            0
                            • mzimmersM mzimmers has marked this topic as solved on

                            • Login

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