In order to use enum enum enumerations in Q ++, you need to create a class inherited from QObject and register it as a QML Type before running the QML engine in the application.
To learn, create a project using QtQuick.
The minimum version of this class with enumerations will be as follows:
Next, you need to register this class as a QML Type in the main function. This requires a QtQml header file and a qmlRegisterType function.
Main.cpp
#include<QGuiApplication>#include<QQmlApplicationEngine>#include<QtQml>// Include QtQml to use qmlRegisterType#include"info.h"// Connect the header file of the Info classintmain(int argc,char*argv[]){ QGuiApplication app(argc, argv);qmlRegisterType<Info>("info",1,0,"Info"); // Register this class as a module QQmlApplicationEngine engine;engine.load(QUrl(QStringLiteral("qrc:/main.qml")));if (engine.rootObjects().isEmpty())return-1;returnapp.exec();}
After that it will only be necessary to connect the new QML module in the main.qml file and use the enumerations to display the text. We will output the message βMessage 0β, βMessage 1β, etc. The numbers will be taken from the enumerations.