PDA

View Full Version : Trouble with QVariant setValue() and Q_DECLARE_METATYPE



tgoetz
24th May 2017, 22:20
I have a small class describing an object which I want to use in a QAbstractItemModel. It's not derived from anything, has a copy and a default constructor and at the end, I'm adding the Q_DECLARE_METATYPE macro which should - per Qt documentation - be sufficient to use the class as a QVariant.



class Dataset
{
Dataset();
Dataset(const Dataset& _data);
~Dataset();
...
}
Q_DECLARE_METATYPE(Dataset)


Here's how I use the class:



...
QVariant var;
Dataset data;
var.setValue(data);
...


When trying to compile, I get the following output/error message:



/usr/include/QtCore/qmetatype.h(169): error: class "QMetaTypeId<Dataset *>" has no member "qt_metatype_id"
static inline int qt_metatype_id() { return QMetaTypeId<T>::qt_metatype_id(); }
^
detected during:
instantiation of "int QMetaTypeId2<T>::qt_metatype_id() [with T=Dataset *]" at line 230
instantiation of "int qMetaTypeId(T *) [with T=Dataset *]" at line 463 of "/usr/include/QtCore/qvariant.h"
instantiation of "void qVariantSetValue(QVariant &, const T &) [with T=Dataset *]" at line 528 of "/usr/include/QtCore/qvariant.h"
instantiation of "void QVariant::setValue(const T &) [with T=Dataset *]" at line 21 of "/home/user/projects/playground/source/Shared/GUI/DatasetTreeModel.cpp"


Any help is greatly appreciated!

d_stranz
25th May 2017, 00:47
If the class declaration code you have posted is your actual code, then none of the members shown are public (there is no "public:" section marker before the constructor). Thus, all of these members are private and inaccessible from outside the class.

The example code you show also won't compile, since you can't create an instance of a class with a private constructor. Most likely, the compilation is failing when you #include the class header file, so it never gets to that point to complain about that.

tgoetz
29th May 2017, 19:59
d_stranz, very true in pointing that out. I probably cut out too much of the class declaration. Of course, the constructors and the destructor are public and there are set/get routines for those members that are private. Any other idea, what might be the cause for the compile error? I've double and triple checked the code for any hint of problem and even reduced the code all the way down to the example given on the Qt website where they introduce this method.

d_stranz
29th May 2017, 22:20
No idea. If you're using Qt Creator, with a .pro file, try running qmake again.