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
As they point out in the Qt forum you have to include <qml.h> (or <QtQml>)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With