"QML_ELEMENT does not name a type" in Qt 5.15
-
wrote on 26 May 2020, 16:03 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?
-
wrote on 26 May 2020, 19:19 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.
-
wrote on 27 May 2020, 17:06 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 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. -
wrote on 27 May 2020, 19:26 last edited by jay1
Did you use "import org.example.ufcs 1.0" in the qml file that you have used ?
-
wrote on 27 May 2020, 20:39 last edited by
@jay1 Yes I did. Sorry, I should've included that in my previous post.
-
wrote on 29 May 2020, 15:20 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 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. -
wrote on 24 Jun 2020, 17:18 last edited by parul1987
I am also getting the same issue . can u plzz share your code
-
wrote on 8 Jun 2021, 12:18 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.