QMetaType alias in Qt6
-
I'm migrating a Qt5.15 client/server to Qt6.5.
QVariant::fromValue()
returns a different type/name for aliased types. The appended sample code returns no longer the typedef nameMyClassList
in Qt6 withusing MyClassList = QList<MyClass>; Q_DECLARE_METATYPE(MyClassList)
Output:
Qt 5.15.2 QVariant::fromValue(MyClassList()).typeName=MyClassList QMetaType(qMetaTypeId<MyClassList>()).name=MyClassList Qt 6.5.0 QVariant::fromValue(MyClassList()).typeName=QList<MyClass> QMetaType(qMetaTypeId<MyClassList>()).name=QList<MyClass>
Code:
#include <QCoreApplication> #include <QList> #include <QMetaProperty> #include <QVariant> class MyClass {}; Q_DECLARE_METATYPE(MyClass); using MyClassList = QList<MyClass>; Q_DECLARE_METATYPE(MyClassList); int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); qInfo("Qt %s", QT_VERSION_STR); QVariant v = QVariant::fromValue(MyClassList()); qInfo("QVariant::fromValue(MyClassList()).typeName=%s", v.typeName()); QMetaType mtMcl = QMetaType(qMetaTypeId<MyClassList>()); qInfo("QMetaType(qMetaTypeId<MyClassList>()).name=%s", qPrintable(mtMcl.name())); return 0; }
-
Is there a way QVariant uses the aliased name
MyClassList
? -
How do I get the Aliased QMetaType?
These two methods still get the wrong type:
QMetaType(qMetaTypeId<MyClassList>())
QMetaType(qRegisterMetaType<MyClassList>("MyClassList"))
Thanks in advance
-
-
@Asperamanca
Q_DECLARE_METATYPE
adds an additionalQMetaType
object with the aliased name to the global metasystem. But the compiler (clang) usesQList<MyClass>
forT
inQVariant::fromValue()
, which results with the wrongQMetaType
. -
@Asperamanca The customized typenames are used in the serialization. Qt5 client doesn't understand
QList<MyClass>
from the Qt6 server (vs). It only recognises the custom types (MyClassList
).My prototype solution is to customize
QVariant::save(QDataStream &s)
where I map the typenames.I still didn't find a method where I can get the
MyClassList
QMetaType object. The debugger shows me the list with all registered names, but how can I iterate/access a specific one? -
@crueegg Maybe qRegisterMetaType can help.
"This function is useful only for registering an alias (typedef) for every other use case Q_DECLARE_METATYPE and qMetaTypeId() should be used instead."
-
@Asperamanca The qRegisterMetaTypeof the
Q_DECLARE_METATYPE
macro is implicitely called inQVariant::fromValue()
.
I'm wondering when/how this alias is/can be retrieved.This is the only possible way I found, but I don't like to have strings as types:
QMetaType mtFn = QMetaType::fromName("MyClassList"); qInfo("QMetaType::fromName(\"MyClassList\").name=%s", qPrintable(mtFn.name()));
Output:
QMetaType::fromName("MyClassList").name=QList<MyClass>
-
@crueegg
Hi, unfortunately, we had to cope with the same issue. The only way to circumvent it was to create a singleton class that handles a mapping between the id of the QMetaType and the custom alias.
See https://gitlab.inria.fr/dtk/dtk-core/-/issues/30 for more details.
Thib.