"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?
-
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 theQML_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 inmain.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 theqmlRegisterType
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. -
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 someqmlRegisterType
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 theQML_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 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.