0

I'm writing a Qt 6.2.0 application running in Ubuntu 20.04. My goal is to add a property to the existing class QtOpcUaNode. So I wrote:

class MyOpcUaNode : public QOpcUaNode
{
    Q_OBJECT

public:
    explicit MyOpcUaNode(QOpcUaNode *parent = nullptr) : QOpcUaNode(parent) { }
    bool updated;
};

But here the first error:

error: no matching function for call to ‘QOpcUaNode::QOpcUaNode(QOpcUaNode*&)’
In file included from ../MyProject/myproject.cpp:1:
../MyProject/myproject.h: In constructor ‘MyOpcUaNode::MyOpcUaNode(QOpcUaNode*)’:
../MyProject/myproject.h:16:73: error: no matching function for call to ‘QOpcUaNode::QOpcUaNode(QOpcUaNode*&)’
   16 |     explicit MyOpcUaNode(QOpcUaNode *parent = nullptr) : QOpcUaNode(parent) { }
      |                                                                         ^

I want to use my class in this way:

QList<MyOpcUaNode *> _listOpcNodes;

_listOpcNodes.append(MyOpcUaNode(_opcUaClient->node("ns=4;s=MEM_RUN_LINE")));
...
_listOpcNodes[x]->updated = true;

Is it correct my approach?

I tried without the copy-constructor but I was not sure how to convert a QOpcUaNode * to MyOpcUaNode *. Trying with:

MyOpcUaNode *node = qobject_cast<MyOpcUaNode *>(_opcUaClient->node("ns=4;s=MEM_RUN_LINE"));

lead node to be a null pointer (even if the _opcUaClient->node was correct).

Mark
  • 4,338
  • 7
  • 58
  • 120
  • QOpcUaNode has no public ctor so you can't derive from it – chehrlic Feb 09 '22 at 11:49
  • And what do you suggest in order to add a property to it, then? Because I need to flag that an element of the list is updated when the `OpcUaNode::attributeRead` signal is emitted. – Mark Feb 09 '22 at 11:51

1 Answers1

1

If you want to add property to class which inherits from QObject, you can do it with setProperty() https://doc.qt.io/qt-5/qobject.html#setProperty. Well, it is not type-safe but it is very simple. Sometimes pragmatism beats purism.