Solved connect with extra argument on slot
-
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 nodeValueUpdatedm_propertyMap.setProperty(key, value.toDouble());
-
Use a lambda as slot
connect(m_nodes.value(key).data(), &QOpcUaNode::dataChangeOccurred, this, [](QOpcUa::NodeAttribute attr) { nodeValueUpdate(attr, whatever you want); });
-
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. -
@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. -
@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.