Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Solved "QML_ELEMENT does not name a type" in Qt 5.15

    QML and Qt Quick
    5.15
    5
    8
    4162
    Loading More Posts
    • 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.
    • F
      freeheeler last edited by

      Hi,

      I just installed Qt 5.15 and wanted to try some of the new features, starting with the new QML type registration macro. Following the documentation here, I added the following to my .pro:

      CONFIG += qmltypes
      QML_IMPORT_NAME = org.example.ufcs
      QML_IMPORT_MAJOR_VERSION = 1
      

      And I added QML_ELEMENT to a class I wanted to register:

      class PCHelper : public QObject
      {
          Q_OBJECT
          QML_ELEMENT
      
          Q_PROPERTY(double setPoint MEMBER mSetPoint NOTIFY setPointChanged)
          ...
      }
      

      I made sure to use the new Qt version (and not 5.14.2 which is also installed), but I still get the error:
      'QML_ELEMENT' does not name a type.

      Also, when I run qmake, I get this warning:
      Warning: Failure to find: ufcs-pc_metatypes.json

      What did I miss?

      1 Reply Last reply Reply Quote 0
      • F
        FKosmale last edited by

        Did you include <qqml.h>? That's where QML_ELEMENT is defined.

        About the qmake warning: That's IIRC an open bug; but the warning is harmless. The json file should be generated once you run make/nmake.

        1 Reply Last reply Reply Quote 1
        • F
          freeheeler last edited by freeheeler

          Thank you, including <qqml.h> solved that error.
          However, another error appeared:

          [...]/ufcs/build-ufcs-pc-Desktop-Qt5.15-Debug/ufcs-pc_qmltyperegistrations.cpp:10: error: guihelper.h: No such file or directory
           #include <guihelper.h>
                    ^~~~~~~~~~~~~
          

          (guihelper.h is the file where I define the class that has the QML_ELEMENT macro)

          I was able to solve this by adding my source folder to the .pro file:

           INCLUDEPATH += src/cpp
          

          It's a little counter-intuitive since I've never needed to add this before to get a Qt project to build but hey, it works.

          Thanks for your help!

          EDIT: actually despite the build succeeding, this didn't work. I hadn't realized I'd left the qmlRegisterType line in main.cpp. If I remove that, the application doesn't open, with a QML error saying that the type does not exist.

          Isn't the QML_ELEMENT macro supposed to replace the qmlRegisterType instruction? Is there anything else that I need to make this work? The documentation doesn't mention anything else as far as I can tell.

          1 Reply Last reply Reply Quote 0
          • J
            jay1 last edited by jay1

            Did you use "import org.example.ufcs 1.0" in the qml file that you have used ?

            F 1 Reply Last reply Reply Quote 0
            • F
              freeheeler @jay1 last edited by

              @jay1 Yes I did. Sorry, I should've included that in my previous post.

              1 Reply Last reply Reply Quote 0
              • F
                freeheeler last edited by

                I think I've solved it. The problem was that I had a mix of methods for exposing C++ parts of the project to QML.
                Specifically, I still had some qmlRegisterType lines in my main.cpp that used the same import name as the one I defined in the .pro. I guess this caused some kind of conflict that resulted in the class with the QML_ELEMENT macro not to be found by the QML side of things.

                So, changing the QML_IMPORT_NAME to something unique (and including that new name in the QML file) solves this problem.

                1 Reply Last reply Reply Quote 0
                • P
                  parul1987 last edited by parul1987

                  I am also getting the same issue . can u plzz share your code

                  1 Reply Last reply Reply Quote 0
                  • Chaba
                    Chaba last edited by

                    I tried to build OpenGL Under QML example (https://doc.qt.io/qt-5/qtquick-scenegraph-openglunderqml-example.html). I get same error with version 5.14.1, but it works fine with version 5.15.2.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post