Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. connect with extra argument on slot
Forum Update on Monday, May 27th 2025

connect with extra argument on slot

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.1k Views
  • 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.
  • A Offline
    A Offline
    adaptine
    wrote on last edited by
    #1

    Currently doing this:

    QString key = "rPos"; // node.key()
    QObject::connect(m_nodes.value(key).data(), &QOpcUaNode::dataChangeOccurred, this, &opcClient::nodeValueUpdated);
    

    Where both signal and slot has the parameters

    QOpcUa::NodeAttribute attr, QVariant value
    

    But in the nodeValueUpdated function I want an extra parameter such that it'll be defined as:

    void nodeValueUpdated(QOpcUa::NodeAttribute attr, const QVariant &value, const QString &key);
    

    Such that I also can pass the variable "key" to the function. How can this be done?
    The key is to be used as following inside nodeValueUpdated

    m_propertyMap.setProperty(key, value.toDouble());
    
    K 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      Use a lambda as slot

      connect(m_nodes.value(key).data(), &QOpcUaNode::dataChangeOccurred,
              this, [](QOpcUa::NodeAttribute attr) { nodeValueUpdate(attr, whatever you want); });
      

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      A 1 Reply Last reply
      6
      • A adaptine

        Currently doing this:

        QString key = "rPos"; // node.key()
        QObject::connect(m_nodes.value(key).data(), &QOpcUaNode::dataChangeOccurred, this, &opcClient::nodeValueUpdated);
        

        Where both signal and slot has the parameters

        QOpcUa::NodeAttribute attr, QVariant value
        

        But in the nodeValueUpdated function I want an extra parameter such that it'll be defined as:

        void nodeValueUpdated(QOpcUa::NodeAttribute attr, const QVariant &value, const QString &key);
        

        Such that I also can pass the variable "key" to the function. How can this be done?
        The key is to be used as following inside nodeValueUpdated

        m_propertyMap.setProperty(key, value.toDouble());
        
        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        @adaptine

        Have you checked this page https://doc.qt.io/qt-5/signalsandslots-syntaxes.html ?
        As the third entry in top table is says:
        Can connect signals to slots which have more arguments than the signal (using default parameters)

        There you can ensure that another non-variable parameter will be added for slot execution.
        For an additional parameter which is changing all the time, you would need a crystal ball statement in the slot routine.
        The solution would be using an additional extended signal in the inheriting class and adding the changing parameter as required.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        4
        • Christian EhrlicherC Christian Ehrlicher

          Use a lambda as slot

          connect(m_nodes.value(key).data(), &QOpcUaNode::dataChangeOccurred,
                  this, [](QOpcUa::NodeAttribute attr) { nodeValueUpdate(attr, whatever you want); });
          
          A Offline
          A Offline
          adaptine
          wrote on last edited by
          #4

          @Christian-Ehrlicher
          Thanks! Was trying to use lambdas earlier but couldt for life of me figure out how to use it. Ended up with:

          QObject::connect(m_nodes.value(node.key()).data(), &QOpcUaNode::dataChangeOccurred,
                              [=](QOpcUa::NodeAttribute attr, QVariant value) { nodeValueUpdate(attr, value, node.key()); });
          

          Which works perfectly.

          @koahnig
          I'm not quite sure what you mean, but my additional parameter is not changing all the time. I'm simply looping a QHash once where the connect-method is called, using the QHash-key as the additional parameter such that the "nodeValueUpdate"-method knows which key-value pair in the QHash which is to be updated on "dataChangeOccurred"-signal.

          K 1 Reply Last reply
          0
          • A adaptine

            @Christian-Ehrlicher
            Thanks! Was trying to use lambdas earlier but couldt for life of me figure out how to use it. Ended up with:

            QObject::connect(m_nodes.value(node.key()).data(), &QOpcUaNode::dataChangeOccurred,
                                [=](QOpcUa::NodeAttribute attr, QVariant value) { nodeValueUpdate(attr, value, node.key()); });
            

            Which works perfectly.

            @koahnig
            I'm not quite sure what you mean, but my additional parameter is not changing all the time. I'm simply looping a QHash once where the connect-method is called, using the QHash-key as the additional parameter such that the "nodeValueUpdate"-method knows which key-value pair in the QHash which is to be updated on "dataChangeOccurred"-signal.

            K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            @adaptine said in connect with extra argument on slot:

            @koahnig
            I'm not quite sure what you mean, but my additional parameter is not changing all the time. I'm simply looping a QHash once where the connect-method is called, using the QHash-key as the additional parameter such that the "nodeValueUpdate"-method knows which key-value pair in the QHash which is to be updated on "dataChangeOccurred"-signal.

            I referred to the official documentation which "allows" the default argument through routine declaration. Those default are argument are static and cannot change at all during runtime.

            The lambda implementation requires newer Qt libs prebuilds because of compiler features.

            Anyway it is good to see that you have a solution.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            2

            • Login

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