Solved Static member function
-
Hi,
I have to create OPC UA client based on OPEN62541 implementation of OPC UA (https://open62541.org/).
Knowledge of OPC UA is not necessary to help me ! :)
I have created a class named MyClient (inherit Qthread).
Client has to read value of string variable (called 'myString') on server.
So I have a function witch monitors "myString" and if "myString" value changes another function (callback) is called.
{ UA_Client_Subscriptions_addMonitoredItem(client, subId,monitorThis,UA_ATTRIBUTEID_VALUE,&callback, NULL, &monId); }
In that 'callback' function im able to get notifications and new values of my "myString" variable.
static void callback(UA_UInt32 handle, UA_DataValue value, void context){
qDebug()<< "new value is : " << value->value; // OK
setString(value->value);// setter for member variable, dont work : invalid use of member 'MyUaClient::plcString1' in static member function ...
}My brobleme is : as the callback function has to be static, inside of it a can't access my class member variables/properties/setter to set the new values...
Does anybody imagin another approach to do this ?
To be able to access variables/properties/setter inside my static method i have to create an instance on MyClient :
static void callback(UA_UInt32 handle, UA_DataValue value, void context){
MyClient client;
qDebug()<< "new value is : " << value->value; // OK
client. setString(value->value);// ok but not accessible from qml; is it possible to setContextProperty("client",&client) on the fly ??
}THX in advance for help !
-
Hi,
From your description, you should pass a pointer to your client object as the context of your callback and then you can use it to update its internal state.
Since it's likely done in a different thread, you should consider using QMetaObject::invokeMethod in order to call your
setString
slot.Hope it helps
-
Hi @SGaist thx for help. I will try this.