0

I have a class defined as this:

cdataentry.h:

#ifndef CDATAENTRY_H
#define CDATAENTRY_H

#include <QObject>
#include <QString>
#include <QVariant>
#include <QtOpcUa>
#include <QMetaType>

#include <cnodetype.h>
#include <cdatastatus.h>

/**
 * @brief   A class providing data and methods to describe a single OPCUA ua
 *          node in the user input table.
 */
class CDataEntry : public QObject
{
    Q_OBJECT

public:

    CDataEntry(const QString& np, QObject* parent = nullptr);
    ~CDataEntry();

    QString nodePath() const;

private:

    /**
     * @brief   Obsolute path to the node on the MDE server
     */
    const QString m_nodePath;

};

Q_DECLARE_METATYPE(CDataEntry); // to be able to store it in QVariant.

#endif // CDATAENTRY_H

I am trying to store a QList<CDataEntry> object in a QVariant. For that end, I have provided the Q_DECLARE_METATYPE(CDataEntry); The problem is that the code doesnt compile, what I get is:

error: no matching function for call to 'QVariant::QVariant(QList<CDataEntry>&)'

What am I missing here?

Łukasz Przeniosło
  • 2,725
  • 5
  • 38
  • 74
  • There is only an overload for `QList&`, so you will need to transform your `QList` into a `QList – Botje Jun 19 '19 at 11:51
  • Possibly related: https://stackoverflow.com/questions/44436386/how-to-convert-qlistt-to-qvariant?rq=1 – Botje Jun 19 '19 at 11:52
  • Both of the provided solutions make the error dissapear, but then I get further errors: `C:\Qt\5.12.3\mingw73_64\include/QtCore/qmetatype.h:804:20: error: use of deleted function 'CDataEntry::CDataEntry(const CDataEntry&)' return new (where) T(*static_cast(t));` Do I need a default constructor or something? – Łukasz Przeniosło Jun 19 '19 at 11:59
  • 1
    Oh, missed that in your code. [QObject has neither a copy constructor nor an assignment operator.](https://doc.qt.io/qt-5/qobject.html#no-copy-constructor-or-assignment-operator) – Botje Jun 19 '19 at 12:04
  • So I need a default constructor and a copy constructor. – Łukasz Przeniosło Jun 19 '19 at 12:05

1 Answers1

2

You need to add default constructor, copy constructor and copy/assignment operator to your QObject subclass.

Like this:

CDataEntry& operator=(const CDataEntry&){}
CDataEntry(QObject* parent = nullptr):QObject(parent){}
CDataEntry(const CDataEntry&){}
//...
CDataEntry(const QString& np, QObject* parent = nullptr)

after that you can use it in QVariant like that:

    CDataEntry test;
    QList<CDataEntry> list;    
    list.append(test);

    QVariant var = QVariant::fromValue<QList<CDataEntry>>( list );
    auto t = var.value<QList<CDataEntry>>();
    qDebug() << t.first().nodePath();
Xplatforms
  • 2,102
  • 17
  • 26