Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

"QML_ELEMENT does not name a type" in Qt 5.15



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



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



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



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



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



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



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


Log in to reply