I need to cast a QSqlRecord to QVariant and back throughout my project. In order to do so I added
Q_DECLARE_METATYPE(QSqlRecord);
in the .h files of the classes which require the casting. I also have a base class from which several children inherit, in this case I assume it is sufficient to include the Q_DECLARE_METATYPE only once in the base class. I therefore have for example:
When I try to run the program like this I get
Redefinition of 'QMetaTypeId<QSqlRecord>
from widgetBaseClass, pointing to the previous declaration in myTableModel. If on the other hand I remove the declaration I get:
static_assert failed "Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system");
From how I understand the workings of Q_DECLARE_METATYPE this means that if i declare it, it result in an error because it was already declared somewhere else, but if I don't declare it I cannot cast from QVariant, because it does not recognise the object as a valid QVariant, what am I missing?
You should place Q_DECLARE_METATYPE(QSqlRecord)
in only one header and then just include it everywhere it is needed.
Q_DECLARE_METATYPE(QSqlRecord)
has to be outside any classes and namespaces.
Ideally, this macro should be placed below the declaration of the class or struct. If that is not possible, it can be put in a private header file which has to be included every time that type is used in a QVariant.