I have a custom struct which I use as a Q_PROPERTY type in a QMediaPlayer derived class. But here's the code:
struct VideoMeta
{
Q_GADGET
Q_PROPERTY(int width MEMBER width)
Q_PROPERTY(...)
....
public:
int width;
...
};
Q_DECLARE_METATYPE(VideoMeta)
class FrameProvider : public QMediaPlayer
{
Q_OBJECT
Q_PROPERTY(VideoMeta videoMeta READ getVideoMeta WRITE setVideoMeta NOTIFY videoLoaded)
VideoMeta m_videoMeta;
...
}
And I use it in a Label:
Label {
text: "Cached frames: " + cacheLoaded + " / " + frameProvider.videoMeta.framecount
}
This works like charm but here comes the twist:
If I copy and paste the declaration of the struct into a separate header file (and obviously included it) with the Q_DECLARE_METATYPE macro, I get the following error:
QMetaProperty::read: Unable to handle unregistered datatype 'VideoMeta' for property 'FrameProvider::videoMeta'
So I have two questions:
- The less important: Why do I need to use the
Q_DECLARE_METATYPEmacro, if the documentation says that I don't need it with theQ_GADGETmacro because it automatically registers the type? - The more important: Why can't I move the declaration into an other header file? What am I missing?
Thanks in advance!
EDIT:
This might be relevant: I use Qt v5.15 in a Visual Studio (MSVC v142) project. (Not in Qt Creator.)
Q_GADGETmain usage is to allow a non QObject type to have introspection.It does not say anything about registering the type.
Also
Q_DECLARE_METATYPEdoes not register a type, but declares it.To register
VideoMetayou need to callqRegisterMetaType<VideoMeta>(). Qt documentation specifically states thatqRegisterMetaType<T>()must be called for a type to work in the Qt property system.See https://doc.qt.io/qt-5/qmetatype.html#qRegisterMetaType-1