Compiler Error with Q_DECLARE_METATYPE
-
Hi fellow Qters,
I have a small class to describe a dataset, which I would like to use in the context of the model/view system and thus my class needs to act like a QVariant. I have read the Qt documentation of the respective classes and my code looks like this (simplified to the basic parts):class Dataset { public: enum DatasetType { TypeA = 0, TypeB = 1, TypeC = 2, TypeD = 3, }; Dataset(); Dataset(const Dataset& _data); ~Dataset(); ... private: ... DatysetType m_type; ... } Q_DECLARE_METATYPE(Dataset)
After declaring Dataset a Metatype, I'm using it in my code like this:
... QVariant var; Dataset data; var.setValue(data); ...
When I'm trying to compile the code, I get this compile error, however:
/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 18 of "/home/goetz/Projects/playground/source/Shared/GUI/DatasetTreeModel.cpp" compilation aborted for /home/goetz/Projects/playground/source/Shared/GUI/DatasetTreeModel.cpp (code 2)
Has anyone seen this before and knows how to get around? I haven't found anything on google, that would help me out.
-
Hi
@wieland suggested
he has this:
Q_DECLARE_METATYPE(Dataset)
but uses this:
QMetaTypeId<Dataset *> ( the :setValue(const T &) [with T=Dataset *] )he needs a Q_DECLARE_METATYPE(Dataset*) too
Can you try that ?
-
Hi,
good idea, but adding Q_DECLARE_METATYPE(Dataset*) didn't help either. The same compiler error persits.
-
I have
#ifndef DATASET_H #define DATASET_H #include <QMetaType> class Dataset { public: enum DatasetType { TypeA = 0, TypeB = 1, TypeC = 2, TypeD = 3, }; Dataset(); Dataset(const Dataset& _data); ~Dataset(); private: DatasetType m_type; }; Q_DECLARE_METATYPE(Dataset) #endif // DATASET_H
and
#include "dataset.h" Dataset::Dataset() { } Dataset::Dataset(const Dataset &_data) { Q_UNUSED(_data) } Dataset::~Dataset() { }
and use it with this:
void MainWindow::on_pushButton_clicked() { QVariant var; Dataset data; var.setValue(data); qDebug() << var.canConvert(QMetaType::Bool); }
Can't reproduce the problem so far.
-
If I do this:
void MainWindow::on_pushButton_clicked() { QVariant var; Dataset * data = new Dataset; var.setValue(data); qDebug() << var.canConvert(QMetaType::Bool); }
I get a similar compile error, but that can be fixed by adding:
Q_DECLARE_METATYPE(Dataset*)
-
Thank you @Wieland
Also tried it in Qt 5.7. mingw and it compiled and ran.Just to be sure. Please delete the build folder completely and rebuild.
-
Just to be sure it's not just a typo, because I've noticed you made the same typo in your cross-posting on qtcentre: there needs to be a semicolon after your class definition (before the QT_DECLARE_METATYPE).
-
It's just a typo in the post. I do have a a semicolon after the class declaration and before the Q_DECLARE_METATYPE macro.
I've also tried to add Q_DECLARE_METATYPE(Dataset) and Q_DECLARE_METATYPE(Dataset*) without success. I still get the same compiler error.
-
@tobiSF
There must be something more we dont see.
Can you try this test project that compiles for me
https://www.dropbox.com/s/60h20wkvacjbwag/DECLAREMETATYPE.zip?dl=0 -
@mrjj, I downloaded the project and I could compile it. But only after I have removed the trailing coma from the enum and after putting a comment around this line
qDebug() << var.canConvert(QMetaType::Bool);
Otherwise the compiler would show the following error message:
mainwindow.cpp(12): error: no instance of overloaded function "QVariant::canConvert" matches the argument list argument types are: (QMetaType::Type) object type is: QVariant qDebug() << var.canConvert(QMetaType::Bool); ^ compilation aborted for mainwindow.cpp (code 2) Makefile:209: recipe for target 'mainwindow.o' failed make: *** [mainwindow.o] Error 2
But I guess the part that counts - the metatype declaration - seems to work.
-
Quick update, I have changed the following line
Dataset* data; QVariant child; child.setValue(data);
to this
Dataset* data; QVariant child = QVariant::fromValue(data);
and now it's compiling fine. Any idea what might cause this?
-
@tobiSF said in Compiler Error with Q_DECLARE_METATYPE:
You have:
Q_DECLARE_METATYPE(Dataset)
but then use a pointer to that type:
Dataset* data; QVariant child = QVariant::fromValue(data);
those are not interchangeable. It's evident even from your original post (as @Wieland noted):
instantiation of "void QVariant::setValue(const T &) [with T=Dataset *]" at line 18 of "/home/goetz/Projects/playground/source/Shared/GUI/DatasetTreeModel.cpp"
The instantiation is with a pointer to
Dataset
not with aDataset
object. -
@kshegunov thanks, as you said, @Wieland pointed it out and I fixed that right away (without updating the code in the initial post, however. The compiler error persisted and only went away after switching from setValue(data) to QVariant::fromValue(data). Is still can't compile the code when I use the setValue method, but with the QVariant::fromValue I can do what I wanted to.
-
@tobiSF said in Compiler Error with Q_DECLARE_METATYPE:
I fixed that right away (without updating the code in the initial post, however
Sorry if I seem thick, but I don't see it. I saw you attempted to declare the pointer as a metatype, but not that you tried calling the function with an actual object. There seems to be some confusion, so I'll try to expand. I'm referring to this error:
instantiation of "void QVariant::setValue(const T &) [with T=Dataset *]" at line 18 of "/home/goetz/Projects/playground/source/Shared/GUI/DatasetTreeModel.cpp"
which suggests you instantiate
QVariant::setValue
with a pointer to the type, not with the actual type you intend to use. Do you mind providing line18 and the few preceding lines ofDatasetTreeModel.cpp
(where the argument passed to the function is declared) exactly as it appears when you get the compile error? Because this error somehow doesn't correspond to the source sample you provided in the OP.PS.
QVariant::setValue
andQVariant::fromValue
do exactly the same thing for non-qt types (i.e. the ones you declare). -
@kshegunov to be more precise, I fixed the metatype declaration:
... Q_DECLARE_METATYPE(Dataset) Q_DECLARE_METATYPE(Dataset*) ...
I was and still am instantiating the QVariant with a pointer to the type and not an actual type, which is why the initial declaration of the type as metatype didn't work. Now that I have both declared as metatype, it works with pointers.
-
Ah, okay, so it's just me being dense.