Solved Export enum from C++ into QML
-
Hey,
I want to have enum in my c++ class and use it in qml but the value of enum elements are undefined.
My header file:
#ifndef CENUM_TEST_H #define CENUM_TEST_H #include <QObject> class C_TEST : public QObject { Q_OBJECT Q_PROPERTY(int MODE MEMBER m_mode NOTIFY modeChanged) private: int m_mode; public: explicit C_TEST(QObject *parent = nullptr); enum MY_ENUM { One=0, Two, Three }; Q_ENUM(MY_ENUM) int mode() const; void setMode(int mode); signals: void modeChanged(); public slots: }; #endif // CENUM_TEST_H
CPP
#include "c_test.h" int C_TEST::mode() const { return m_mode; } void C_TEST::setMode(int mode) { if(m_mode != mode) { m_mode = mode; emit modeChanged(); } } C_TEST::C_TEST(QObject *parent) : QObject(parent) { setMode(2); }
My main.cpp:
int main(int argc, char *argv[]) { #if defined(Q_OS_WIN) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication app(argc, argv); C_TEST test; QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("COBJECT",&test); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
finally my qml document:
import QtQuick 2.9 Rectangle { id:rectID property alias textField: textID property int mode: COBJECT.MODE x:10*index y:index*height width:45; height:25 color: "red" Text { anchors.fill: parent id: textID text: { switch(mode) { case COBJECT.One: qsTr("1") break; case COBJECT.Two: qsTr("2") break; case COBJECT.Three: qsTr("3") break; default: qsTr("NONE"); } } } Item { focus: true Keys.onPressed: { rectID[index].textField.text = "hey" } } }
Extra Information:
OS:Ubuntu 16.04LTS
QT SDK: 5.10.0
QT CREATOR:4.5.0I appreciate your help.
Yours, -
@Alien hi,
IIRC you'll have to register the enums seperately in your main.cpp usingqmlRegisterUncreatableType
something along this:
qmlRegisterUncreatableType<C_TEST >("CppEnums",1,0,"Enums","Enum is not a type"); //qml import CppEnums 1.0 ... Text { anchors.fill: parent id: textID text: { switch(mode) { case Enums.One: qsTr("1") break; case Enums.Two: qsTr("2") break; case Enums.Three: qsTr("3") break; default: qsTr("NONE"); } } }
you can also make your Q_Property directly of your enum type without the cast into int
Q_PROPERTY(MY_ENUM MODE MEMBER m_mode NOTIFY modeChanged)
-
Thank you for your solution that works fine but I think that I've to use both registration please look at the below code
engine.rootContext()->setContextProperty("COBJECT",&test); qmlRegisterUncreatableType<C_TEST >("CppEnums",1,0,"Enums","Enum is not a type");
Since I need the mode variable (this variable will change time to time)
does this usage have any problems? -
@Alien said in Export enum from C++ into QML:
Thank you for your solution that works fine but I think that I've to use both registration please look at the below code
engine.rootContext()->setContextProperty("COBJECT",&test); qmlRegisterUncreatableType<C_TEST >("CppEnums",1,0,"Enums","Enum is not a type");
Since I need the mode variable (this variable will change time to time)
you need the contxtProperty too, the qmlRegisterUncreatableType just makes your Enum(-class) available to your qml files you still need a reference object from your cpp-backend.
does this usage have any problems?
I don't think so, I use it the same way.
-
This post is deleted!