Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Static member function
Qt 6.12 LTS is out! See what's new in the release blog

Static member function

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by
    #1

    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 !

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        Hi @SGaist thx for help. I will try this.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt
        • Unsolved