Search code examples
qtqmlqt5

Unknown type name QML_ELEMENT


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

Solution

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