Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown type name QML_ELEMENT

Tags:

qt

qt5

qml

In the new Qt 5.15.0 there is a new declarative way to register C++ types for usage in QML. I followed the steps given in Qt Help (https://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html#registering-an-instantiable-object-type) but it givens to me the following error:

/.../randomnumbergenerator.h:10: error: ‘QML_ELEMENT’ does not name a type
     QML_ELEMENT
     ^~~~~~~~~~~

The class's definition is, for the moment:

#ifndef RANDOMNUMBERGENERATOR_H
#define RANDOMNUMBERGENERATOR_H

#include <QObject>
#include <QRandomGenerator>

class RandomNumberGenerator : public QObject
{
    Q_OBJECT
    QML_ELEMENT
    QML_SINGLETON

public:
    explicit RandomNumberGenerator(QObject *parent = nullptr);

signals:

};

#endif // RANDOMNUMBERGENERATOR_H

EDIT: I already added to the .pro file the following:

CONFIG += c++11 qmltypes
QML_IMPORT_NAME = SimpleRng
QML_IMPORT_MAJOR_VERSION = 1
like image 698
lateus Avatar asked Jul 03 '26 07:07

lateus


2 Answers

As they point out in the Qt forum you have to include <qml.h> (or <QtQml>)

like image 156
eyllanesc Avatar answered Jul 07 '26 16:07

eyllanesc


The preconditions section of the documentation you quoted states that

All the macros mentioned below are available from the qqml.h header. You need to add the following code to the files using them in order to make the macros available:

#include <QtQml/qqml.h>

That seems to be the canonical answer and may avoid the issues that @lateus mentioned in comments.

like image 31
Kasdonchu Avatar answered Jul 07 '26 16:07

Kasdonchu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!