【发布时间】:2019-06-19 20:17:49
【问题描述】:
我有一个这样定义的类:
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
我正在尝试将QList<CDataEntry> 对象存储在QVariant 中。为此,我提供了Q_DECLARE_METATYPE(CDataEntry);
问题是代码无法编译,我得到的是:
error: no matching function for call to 'QVariant::QVariant(QList<CDataEntry>&)'
我在这里错过了什么?
【问题讨论】:
-
只有
QList<QVariant>&的重载,因此您需要先将QList<CDataEntry>转换为QList<QVariant。 -
提供的两种解决方案都使错误消失,但随后我得到更多错误:
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<const T*>(t));我需要默认构造函数吗? -
所以我需要一个默认构造函数和一个拷贝构造函数。