I'm having trouble creating a QVariant
with a custom type. Here is a small example that shows what I want to achieve:
main.cpp:
#include "MyClass.h"
int main() {
MyClass some_object;
QVariant variant(some_object);
return 0;
}
include/MyClass.h:
#pragma once
#include <QtCore>
class MyClass {
public:
MyClass() : _my_member(0.0) {}
MyClass(const MyClass &other) { _my_member = other._my_member; }
~MyClass() {}
MyClass& operator=(MyClass& other)
{
swap(*this, other);
return *this;
}
MyClass(MyClass&& other) : MyClass()
{
swap(*this, other);
}
friend void swap(MyClass& first, MyClass& second)
{
using std::swap;
swap(first._my_member, second._my_member);
}
private:
float _my_member;
};
Q_DECLARE_METATYPE(MyClass);
The build fails with the following error:
error: no matching function for call to ‘QVariant::QVariant(MyClass&)’
QVariant variant(some_object);
^
How can I solve this error?