Questions about Q_PROPERTY and migration (Qt5 -> Qt6)
-
Hi,
I am migrating the Qt version of my application (from 5.15 to 6.4).
And I have compilation errors and one in particular :Type argument of Q_PROPERTY or Q_DECLARE_METATYPE(T*) must be fully defined
I found some links that explain why this error appears :
- What's new in QMetaType + QVariant
- Qt6 regression with QAbstractListModel? Type argument of Q_PROPERTY or Q_DECLARE_METATYPE(T*) must be fully defined (Qt Forum)
But I didn't understand everything... So I have many questions
1. What is an opaque pointer ?
My hypothesis is that it is a pointer to an object whose behaviour is unknown (like a class in a library where we only have access to the header). Therefore, a pointer to a known class (that we have created and that is in the same module for example) is not an opaque pointer.
Am I right ?2. If I have a custom object like in this code :
Q_PROPERTY(Camera activeCamera READ getActiveCamera NOTIFY activeCamerasChanged)
I need to replace
include "Camera.h"
byQ_MOC_INCLUDE("Camera.h")
. Is it correct ?3. If I have a pointer of a custom object
Q_PROPERTY(Camera* activeCamera READ getActiveCamera NOTIFY activeCamerasChanged)
Should I need to replace the
Q_MOC_INCLUDE
by aQ_DECLARE_OPAQUE_POINTER
?#ifndef OPAQUE_Camera #define OPAQUE_Camera Q_DECLARE_OPAQUE_POINTER(Camera*) #endif // OPAQUE_Camera
4. If I have a QList of pointer
Q_PROPERTY(QList<Camera*> cameras READ getCameras NOTIFY camerasChanged)
Is the previous
Q_DECLARE_OPAQUE_POINTER
correct ?5. If I have a
Camera
object and aQList<Camera*>
, what should I use ?Thanks for your help !
-
@mipr said in Questions about Q_PROPERTY and migration (Qt5 -> Qt6):
[...]
1. What is an opaque pointer ?
My hypothesis is that it is a pointer to an object whose behaviour is unknown (like a class in a library where we only have access to the header). Therefore, a pointer to a known class (that we have created and that is in the same module for example) is not an opaque pointer.
Am I right ?No. Opaque pointer is a pointer about which compiler knows nothing apart from name. In other words: a pointer to forward-declared type.
For example:
class QString; QString *something;
In this example,
QString
is forward-declared. The compiler knows only it's type (it's a class) and name ("QString"), but knows nothing else (not even methods inside of it likeQString::isEmpty()
).Typically you will have forward-declared types in headers (to speed up compilation time) and only actually include the full data (
#include <QString>;
) in the source file.In Qt 6, types of Qt properties must be fully defined. So, in simplest terms:
class QString; // BAD in Qt6! #include <QString>; // GOOD in Qt6! Q_PROPERTY(QString something ...)
2. If I have a custom object like in this code :
Q_PROPERTY(Camera activeCamera READ getActiveCamera NOTIFY activeCamerasChanged)
I need to replace
include "Camera.h"
byQ_MOC_INCLUDE("Camera.h")
. Is it correct ?No need to replace anything. A non-pointer has to be fully declared anyway so no change is necessary.
3. If I have a pointer of a custom object
Q_PROPERTY(Camera* activeCamera READ getActiveCamera NOTIFY activeCamerasChanged)
Should I need to replace the
Q_MOC_INCLUDE
by aQ_DECLARE_OPAQUE_POINTER
?#ifndef OPAQUE_Camera #define OPAQUE_Camera Q_DECLARE_OPAQUE_POINTER(Camera*) #endif // OPAQUE_Camera
No, it should be enough to use either
#include
(if you don't care about forward declarations) orQ_MOC_INCLUDE
.4. If I have a QList of pointer
Q_PROPERTY(QList<Camera*> cameras READ getCameras NOTIFY camerasChanged)
Is the previous
Q_DECLARE_OPAQUE_POINTER
correct ?I don't know.
5. If I have a
Camera
object and aQList<Camera*>
, what should I use ?I don't understand the question, sorry.