How and when to use Q_DECLARE_METATYPE

I need to pass QSqlRecord to and from QVariant throughout my project. For this, I added

Q_DECLARE_METATYPE(QSqlRecord);

in .h class files requiring casting. I also have a base class from which several children inherit, in this case I assume that it is enough to include Q_DECLARE_METATYPE only once in the base class. Therefore, I have, for example:

  • widgetBaseClass: declares a metatype
  • widgetChildClass1: inherits widgetBaseClass does not declare a metatype
  • widgetChildClass2: inherits widgetBaseClass does not declare a metatype
  • myTableModel: declares a metatype

When I try to run such a program, I get

Redefinition of 'QMetaTypeId<QSqlRecord>

from widgetBaseClass, pointing to the previous declaration in myTableModel. If, on the other hand, I delete the declaration I receive:

static_assert failed "Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt meta-object system");

, Q_DECLARE_METATYPE, , , , ​​- , , QVariant, QVariant, ?

+4
2

Q_DECLARE_METATYPE (QSqlRecord) , . Q_DECLARE_METATYPE (QSqlRecord) . Qt:

. , , , QVariant

+4

Q_DECLARE_METATYPE , , copy-constructor, QSqlRecord . , . , #pragma once?

, :

//mystruct.h
namespace MyNamespace
{
struct MyStruct
{
    int i;
    ...
};
}

Q_DECLARE_METATYPE(MyNamespace::MyStruct)

: http://doc.qt.io/qt-5/qmetatype.html#Q_DECLARE_METATYPE

0

Source: https://habr.com/ru/post/1672444/


All Articles