Enum/Flags from C++ to QML
-
From here and here I'm trying to expose to QML this enum (or flag following the Qt meaning):
settings.h
#ifndef SETTINGS_H #define SETTINGS_H #include <QObject> class Settings : public QObject { Q_OBJECT public: enum GestureType { Unknown, Left, Right, Up, Down, Far, Near }; Q_DECLARE_FLAGS(GestureTypes, GestureType) Q_FLAG(GestureTypes) explicit Settings() { } }; #endif // SETTINGS_H
main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQmlContext> #include "settings.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; Settings settings; const QUrl url(QStringLiteral("qrc:/main.qml")); QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.rootContext()->setContextProperty("settings", &settings); engine.load(url); return app.exec(); }
main.qml
console.log(settings.Near);
is
undefined
, the same if I try to usesettings.GestureTypes.Near
.Why?
-
You have to register the Setting type to QML
-
you need to register your enum:
qmlRegisterUncreatableType<Settings> ("SettingsEnumImport", 1, 0, "NameSpace", "Some Message");
where you want to use it in qml:
import SettingsEnumImport 1.0 ... ... console.log(NameSpace.Near)
not sure if it will work with the Q_FLAG macro, but it will with Q_ENUM(GestureTypes)
-
@J-Hilk it seems to work, thanks! But in the links I provided there is no mention of
qmlRegisterUncreatableType
nor about the needs of import the domain. How should I guess from the docs? It's a real question, because if I understand how to read the docs I would solve most of the problems just reading the manual... -
@Mark81 said in Enum/Flags from C++ to QML:
engine.rootContext()->setContextProperty("settings", &settings);
What then this line does?
As described in documentation, this will define a property called "settings" in the root context.
This property will be linked to local variablesettings
.qmlRegisterUncreatableType<Settings> ("SettingsEnumImport", 1, 0, "NameSpace", "Some Message");
will register a new type calledNameSpace
in namespaceSettingsEnumImport 1.0
. This type in uncreatable, which means you cannot create a property/variable with type. -
@Mark81 said in Enum/Flags from C++ to QML:
I provided there is no mention of qmlRegisterUncreatableType nor about the needs of import the domain
mmh 🤔, I remember that differently. I actually found this solution myself in one of the official Qt examples.
But even that I'm unable to find right now. Maybe the method changed, or was supposed to change / now work without a call to qmlRegisterUncreatableType
-
@Mark81 said in Enum/Flags from C++ to QML:
But in the links I provided there is no mention of qmlRegisterUncreatableType nor about the needs of import the domain
Then you don't have read carefully the documentation.
To get access to enum on QML side, you have to register it (cf. Enumeration Type documentation)Providing the Message class has been registered with the QML type system.
The given link is Registering C++ Types with the QML Type System
There are multiple way to achieve this, depending on Qt Version you are using and build system (qmake or cmake).
But the way exposed by @J-Hilk works in every case.