I'm trying to save a custom type to QSettings but I'm getting an error at runtime. Here's the class I'm trying to save:
#ifndef TESTCLASS_H
#define TESTCLASS_H
#include <QMetaType>
#include <QString>
class TestClass
{
public:
QString testString;
int testInt;
bool testBool;
};
Q_DECLARE_METATYPE(TestClass)
#endif
And here's the code to save an instance of the class to QSettings
TestClass test;
test.testString = "Test";
test.testInt = 10;
test.testBool = false;
settings.setValue("TestGroup/TestVal", QVariant::fromValue(test));
settings.sync();
The error I get at runtime is:
QVariant::save: unable to save type 'TestClass' (type id: 1032).
ASSERT failure in QVariant::save: "Invalid type to save", file kernel\qvariant.cpp, line 2124
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
According to the documentation, the class must provide a default constructor, destructor and a copy constructor. For this class the automatically generated constructor, destructor and copy constructors would be sufficient, so I haven't provided one (though I did try it anyway to see if that was the problem). I've also used Q_DECLARE_METATYPE() so the class is known to QMetaType, so as far as I can tell I've met the requirements to use the class with QVariant.
What am I missing?