Working wit C++ Backends, how acces Qml Object
-
Hello I´m new with QML and develop a embedded application.
To access the QML Properties I wrote backends in C++.//Qml file ```import einrichtbackend 1.0 SpinBox { id: spinBox_proben_nr x: 350 y: 30 width: 180 height: 40 wheelEnabled: false editable: true to: 9999 value: einrichtbackend.nummer onValueChanged: einrichtbackend.setNummer(value)}```
``` // in my main: qmlRegisterType<EinrichtBackEnd>("einrichtbackend",1 ,0, "EinrichtBackEnd");
//C++ Backend Header #include <QObject> class EinrichtBackEnd : public QObject { Q_OBJECT Q_PROPERTY(int nummer READ nummer WRITE setNummer NOTIFY NummerChanged) private: static int m_Nummer; //Static so every Object has the same Value public: int nummer() const; signals: void NummerChanged(int nummer); public slots: void setNummer(int Nummer); } ;
//C++ Backend cpp //global Object EinrichtBackEnd o_einrichtbackend; // intialize Variable int EinrichtBackEnd::m_Nummer = 1; EinrichtBackEnd::EinrichtBackEnd(QObject *parent) : QObject(parent) { } int EinrichtBackEnd::nummer() const { return m_Nummer; } void EinrichtBackEnd::setNummer(int Nummer) { if (m_Nummer == Nummer) return; m_Nummer = Nummer; emit NummerChanged(m_Nummer); }
With the global Object I can acces from any File to the Qml Interface.
My Problem is that I have two Objects one from C++ and one from qml.
With the c++ object I can change the members but the signals to qml doesn´t work.My Question:
Can I acces in C++to the qml Object or how can I say with my c++ object to the qml interface that the values have changed?Sorry for my bad english
Thanks for your help. -
hi @Peppi9300 said in Working wit C++ Backends, how acces Qml Object:
//Qml file
import einrichtbackend 1.0 SpinBox { id: spinBox_proben_nr x: 350 y: 30 width: 180 height: 40 wheelEnabled: false editable: true to: 9999 value: einrichtbackend.nummer onValueChanged: einrichtbackend.setNummer(value)}
The way it is currently, it shouldn't work, you have the c++ class as a RegisteredType -> you need an actual object of it in your QML file
import einrichtbackend 1.0 SpinBox { id: spinBox_proben_nr x: 350 y: 30 width: 180 height: 40 wheelEnabled: false editable: true to: 9999 EinrichtBackEnd{ id: backEnd } value: backEnd.nummer onValueChanged: einrichtbackend.setNummer(value)}
now, the qml side is updated when ever nummerChanged() is emited from your c++ file and inside the cpp class nummerChanged() is also emitted, when the QML file changes the nummer property (backEnd.nummer = 1112324)
PS:
Nice Denglisch nomenclature you have there x) -
@J-Hilk thanks for your reply.
The Object in the qml I only forget to post in my question.My Problem is that I want to use the o_einrichtbackend which is a global Object in my Backend to make changes in my GUI.
But the NummerChanged Signal does not work.
Can I acces the Object from QML in C++? -
"My Problem is that I have two Objects one from C++ and one from qml."
No, you don't. At least I can't see where the object is instantiated in QML!
Before doing something like that:value: einrichtbackend.nummer
you've got to instantiate the object like so:
EinrichtBackEnd { id: einrichtbackend }
The line:
qmlRegisterType<EinrichtBackEnd>("einrichtbackend",1 ,0, "EinrichtBackEnd");
in your main.cpp file only registers the type in QML. Now QML knows only the type, it doesn't instantiate the object for you!
Anyway, if you already instantiated the global object "o_einrichtbackend" in C++, you can inject it into the root context of QML:
// qmlRegisterType<EinrichtBackEnd>("einrichtbackend",1 ,0, "EinrichtBackEnd"); engine.rootContext()->setContextProperty(QStringLiteral("einrichtbackend"), o_einrichtbackend);
Now the object "einrichtbackend" is known in the QML context and you can use it the way you already did.
P.S.: Englisch und Deutsch gemischt in Klassen-/Variablennamen ist bäh! ;-)