Accessing ContextProperty from cpp
-
Hello !
Im having troubles to access an object that i putted in my rootContextProperties like this :
/Main.cpp/
MyUaClient client;
engine.rootContext()->setContextProperty("client",&client);
/************/-Now from my QML files i can access 'client' writing : client.name / client.age...
-But how to access that client instance from an other C++ class ?
Thx in advance for help,
Levon
-
@LeLev said in Accessing ContextProperty from cpp:
-But how to access that client instance from an other C++ class ?
the same way you've set it.
Means there is also an according getter method available. You can find it in the docs. -
Hi @raven-worx thx for the answer ,
I am looking at 'http://doc.qt.io/qt-5/qqmlcontext.html', I can not find anything... Could you please send me a link ?
This is my class where I want to access the rootContextProperty :
class MyUaCallback : public QObject
{
Q_OBJECT
private :
QUrl mainUrl = "main.qml";public:
MyUaCallback();/* "static void callback" is the function witch will modify context property */
static void callback(UA_UInt32 handle, UA_DataValue *value, void* context,QUrl url){ QQmlApplicationEngine* engine = new QQmlApplicationEngine(url); //QQmlContext* context = engine->rootContext(); MyUaClient *cl; cl = engine->rootObjects()[0]; cl->setPlcString1("newStringValue"); // "setPlcString1" is method of 'MyUaClient' qDebug()<< "ContextProperty modified"; }
};
But this is not working
Thx for help,
Levon -
@LeLev said in Accessing ContextProperty from cpp:
I am looking at 'http://doc.qt.io/qt-5/qqmlcontext.html', I can not find anything... Could you please send me a link ?
Really?! Come on.... QQmlContext::contextProperty()
When you would have at least tried to look over the method summary on the top of the page you would for sure had found it.... -
@raven-worx thx. Sorry i was looking for something starting with 'get' ...
-
@LeLev Just like any other object in any C++ object? You have client available in main.cpp so you can do e.g.
MySomethingOther otherObject; MyUaClient client; otherObject->setClient(&client); // set as context property...
or
MyUaClient client; MySomethingOther* otherObject = new MySomethingOther(&otherObject); // set as context property...
To have access to an object which have been set as a context property you don't need to use the qml engine or context.
But after seeing your second post I'm confused about what you actually meant and need...
-
Hello @Eeli-K ,
As i said in the first post, i have a 'client' object instance that i have putted in my rootContextProperties like this :
//
MyUaClient client;
engine.rootContext()->setContextProperty("client",&client);
//
I need this to access my client member variables from QML. OK.Now i just want to modify my clients member variables from another c++ class, so i need to get that particular client instance..
Is this more clear ?
Thx
-
@LeLev I believe it's clear, and as I said, it's just another C++ object for your other C++ object and you can bypass the qml engine and contexts altogether. I, for example, have this code:
Tools* tools{new Tools()}; ... QQmlApplicationEngine* engine{new QQmlApplicationEngine()}; QQmlContext* ctx{engine->rootContext()}; Connection* conn{new Connection()}; ProfileData* data{new ProfileData(ctx, conn)}; Monitoring* monitoring{new Monitoring(conn)}; CameraController* camera{new CameraController(conn)}; conn->getNetworks(); qmlRegisterUncreatableType<CameraController>("eu.vpsystems.software", 1, 0, "CameraController", "Not to be created as QML type"); ctx->setContextProperty("qApplication", app); ctx->setContextProperty("profileData", data); ctx->setContextProperty("connection", conn); ctx->setContextProperty("tools", tools); ctx->setContextProperty("monitoringData", monitoring); ctx->setContextProperty("camera", camera); engine->load(QUrl(QStringLiteral("qrc:/main.qml")));
Notice how connection is used in the constructors of other objects; it's just a member object for those objects. Each object can be used in QML. The root context is passed to one object because it sets other dynamically created objects as context properties. I don't need to get any object from the root context.