mercredi 8 août 2018

Qt how to extract custom data pointer from QVariant without knowing the internal type

I have a QVariantMap with data. Inside the QVariant I can store custom class declared and registered in meta-system.

I know does exists QVariant::value<XXX>() but since i don't know a-priori what XXX is I cannot do that.

So I tried to convert to QObject* both with QVariant::value<QObject*> and qvariant_cast but it seems to hold a void pointer:

My attempt:

MyClass* obj = new MyClass();
QVariant variant = QVariant::fromvalue<MyClass*>(obj);  // it works;

qDebug() << variant; // Qvariant(MyClass*,)

QObject* obj2 = variant.value<QObject*>();

qDebug() << obj2; // QObject(0x0)

Aucun commentaire:

Enregistrer un commentaire