Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to convert QVariant(CustomType *) to CustomType*



  • Im passing a map object into QML by using ;
    @viever.rootContext()->setContextProperty(.. , ..)@

    My map object is ;
    @QMap<QString, QVariant> map;
    map.insert("KEY", QVariant::fromValue(CustomTypeList*))@

    In QML im getting @QVariant(CustomTypeList*)@ by MyMapObject["KEY"]. It is the model for my repeater. It is ok but i have to reach an invokable method (insert Method) in order to manupulate this model object. My question is that how can i reach CustomType's methods from QVariant(CustomType*)?

    or any other suggestion?


  • Moderators

    @
    QVariant variant = QVariant::fromValue(CustomTypeList *object);
    CustomTypeList *myVariable = variant.value<CustomTypeList *>();
    @



  • thx for answer sierdzio, but i have to do this convertion in javascript side. Is there any way to do it?


  • Moderators

    AFAIK, JavaScript will not understand your custom type, so it cannot convert.



  • hmm.. but when i write the typeof my variant object, it will write on log screen "QVariant(CustomObject*)"

    im using
    @console.log(myVariantObj["KEY"])@
    The result is
    @QVariant(CustomType*)@


Log in to reply