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. Using the QML_ELEMENT macro properly to avoid error "use of undeclared identifier"
Forum Updated to NodeBB v4.3 + New Features

Using the QML_ELEMENT macro properly to avoid error "use of undeclared identifier"

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 579 Views
  • 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.
  • KoryK Offline
    KoryK Offline
    Kory
    wrote on last edited by Kory
    #1

    Hello, I suppose it might be best practice to use QML_ELEMENT macro instead of manually registering our C++ classes in QML with qmlRegisterType based on this blog post from Qt

    https://www.qt.io/blog/qml-type-registration-in-qt-5.15

    Our current set-up is this

    Main.cpp file has registration of our class

    #include "auth/login_manager.hpp"
    
      qmlRegisterType<auth::LoginManager>(
      "dancesyllabuses_loginManager", 1, 0, "LoginManager");
    

    Our class is like this

    namespace auth {
    class LoginManager : public QObject {
      Q_OBJECT
    };
    } // namespace auth
    

    Now let's say, we want to add our macro

    We would change our class like so

    #include <QtQml>
    
    namespace auth {
    class LoginManager : public QObject {
      Q_OBJECT
      QML_ELEMENT
    };
    } // namespace auth
    

    Then in theory, we no longer need qmlRegisterType in our main.cpp file

    And I'm assuming we also no longer need to include the header file for it too in our main.cpp file too. But let me know if I'm wrong about that.

    Regardless, however, here's where I run into a problem

    In the auto generated file, the program complains

    appdance_syllabuses_lib_qmltyperegistrations.cpp:23:34: error: use of undeclared identifier 'auth'
        qmlRegisterTypesAndRevisions<auth::LoginManager>("Dance_Syllabuses", 1);
                                     ^
    

    1b59aeac-ecf4-4acb-9886-051fc56f25ff-image.png

    Perhaps the problem is a result of using namespaces like Qt doesn't like that

    Perhaps the problem is something I've missed with transitioning to using our macro like I might have had to do more things within our class or to cmakelists.txt or something. Let me know if it's programmer mistake!

    Any help with getting past this issue would be greatly appreciated! =)

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Redman
      wrote on last edited by Redman
      #2

      If you are using Cmake then you have to tell where to find files that implement classes using QML_ELEMENT macro

      # Add include directories so target can find the QMLELEMENTS
      set(QML_INCLUDES
          ${CMAKE_CURRENT_SOURCE_DIR}/path/to/src
      )
      
      # Add includepaths
      target_include_directories(target PRIVATE
         ${QML_INCLUDES}
      )
      
      KoryK 2 Replies Last reply
      0
      • R Redman

        If you are using Cmake then you have to tell where to find files that implement classes using QML_ELEMENT macro

        # Add include directories so target can find the QMLELEMENTS
        set(QML_INCLUDES
            ${CMAKE_CURRENT_SOURCE_DIR}/path/to/src
        )
        
        # Add includepaths
        target_include_directories(target PRIVATE
           ${QML_INCLUDES}
        )
        
        KoryK Offline
        KoryK Offline
        Kory
        wrote on last edited by
        #3

        @Redman Thanks, that's working now! =)

        1 Reply Last reply
        0
        • KoryK Kory has marked this topic as solved on
        • R Redman

          If you are using Cmake then you have to tell where to find files that implement classes using QML_ELEMENT macro

          # Add include directories so target can find the QMLELEMENTS
          set(QML_INCLUDES
              ${CMAKE_CURRENT_SOURCE_DIR}/path/to/src
          )
          
          # Add includepaths
          target_include_directories(target PRIVATE
             ${QML_INCLUDES}
          )
          
          KoryK Offline
          KoryK Offline
          Kory
          wrote on last edited by Kory
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • KoryK Kory has marked this topic as unsolved on
          • KoryK Offline
            KoryK Offline
            Kory
            wrote on last edited by Kory
            #5

            Yeah that's all working now

            I was receiving some errors

            W/default : qrc:/Main.qml:10:1: static plugin for module "URI " with name "URIPlugin" cannot be loaded: Namespace 'URI' has already been used for type registration.
            
            GlobalContextModule/pages/SignUp.qml:204 RolesList is not a type
            

            Solution to this I believe was to

            • Make sure I hadn't repeated the import statement for our URI from the Parent cmakelists.txt. Where URI refers to URI of our target that we provided to target_include_directories. And our URI is declared in qt_add_qml_module
            • Move the RolesList{} QML element from page in child module to Main.qml where our import statement was. Even though Main.qml loads this page in and in effect, should provide it with what it wants, it didn't for me in practice. Before we used QML_ELEMENT I had an import statement just for RolesList{} in page of child module but now that thing is in our Import URI in Main.qml. If that doesn't make any sense, basically all I'm saying is don't assume classes put inside of main module via QML_ELEMENT and CmakeLIsts.txt get passed down to child modules included into this main module
            1 Reply Last reply
            0
            • KoryK Kory 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