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

Casting a custom class to QVariant (2020 but still relevant)



  • Re: Casting a custom class to QVariant

    I want to use a QVariantMap with a custom class (based upon QObject). Your earlier answers to a similar thread said to use this.

    QVariant v = QVariant::fromValue<cMyClass>(MyObject);

    When I do this the compiler complains that I need to use Q_DECLARE_METATYPE(cMyClass). However, when I do that, I get another compiler error about using a deleted function. This seems to be related to using QObject. So, very much like the user Onyx back in 2018 I'm stuck because I can't get the value into QVariant without causing a compiler error.


  • Lifetime Qt Champion

    @Doug-Beck said in Casting a custom class to QVariant (2020 but still relevant):

    using a deleted function

    Can you post exact error?
    Do you have Q_OBJECT macro in your class?


  • Lifetime Qt Champion

    @Doug-Beck said in Casting a custom class to QVariant (2020 but still relevant):

    I want to use a QVariantMap with a custom class (based upon QObject).

    This will not work since QObject is not copyable.



  • @Christian-Ehrlicher So, QVariantMap only works with data types that aren't based upon QObject? I could really use the ability to map a QString onto a custom class based upon QObject. Ultimately, what I'd like to do is have a QML combo box with a key property and use that key property to look up the model and current index. Is there a better way to do this? I'm still learning the basics of Qt.


  • Lifetime Qt Champion

    Hi
    To used classes based on QObject, you must store them as pointers.
    QObject itself cannot be copied and hence cannot be stored in any container
    as plain type. Only via pointers.



  • I'm making some progress. I used QMap like this QMap<QString, MyClass*> as a QProperty in my class. Then, in the ComboBox, I used this. I have a property in the QML file called "controlName".

                ComboBox {
                    model: MainViewMgr.controls[controlName].nodeList
                    width: 132
    
                    onActivated: {
                        MainViewMgr.controls[controlName].currentIndex = currentIndex;
                    }
    
                    onModelChanged: {
                        currentIndex = Qt.binding( function(){return MainViewMgr.controls[controlName].currentIndex} )
                    }
                }
    

    However, I get this error. Does this mean the Javascript doesn't support using maps?

    TypeError: Cannot read property 'nodeList' of undefined



  • I have seen some posts on-line that suggest I should be using QVariantMap instead of QMap but I wasn't able to get that to compile with a class based upon QObject.


  • Lifetime Qt Champion

    Hi
    Yes https://doc.qt.io/qt-5/qtqml-cppintegration-data.html mentions
    QVariantMap so that should work.

    What error did you get with QVariantMap ?
    Also you must also use pointers to "cMyClass"
    And it should be supported
    https://doc.qt.io/qt-5/qmetatype.html#Type-enum
    as it has QMetaType::QObjectStar



  • @mrjj The problem is getting myClass into the QVariant. Using the following required me to use Q_DECLARE_METATYPE.

    QVariant v = QVariant::fromValue<cMyClass>(MyObject);

    However, as soon as I use Q_DECLARE_METATYPE, I get errors about using a deleted function. Without this reference, it compiles just fine. But I can't see a method to insert myClass into QVariantMap without using QVariant::fromValue.


  • Lifetime Qt Champion

    Again: you can't copy a QObject! use a pointer...



  • @Christian-Ehrlicher There's something I'm not understanding here. Here's my class. When I uncomment the part about Q_DECLARE_METATYPE, I immediately get the compiler error with the use of deleted function.

    #include <QObject>
    // #include <QMetaType>
    #include "PropertyHelper.h"

    class cMyClass: public QObject
    {
    Q_OBJECT
    AUTO_PROPERTY(QString, nodeValue)
    AUTO_PROPERTY(QString, nodeUnits)
    AUTO_PROPERTY(QStringList, nodeList)
    AUTO_PROPERTY(qint16, currentIndex)
    public:
    explicit cMyClass(QObject* parent = nullptr);
    explicit cMyClass(QObject* parent, QString controlName, QString value, std::vector<QString> nodeStringVector);
    QString controlName() const {return mControlName;}

    private:
    QString mControlName;
    };

    // Q_DECLARE_METATYPE(cMyClass);

    In my ViewMgr class, I used this as my Q_PROPERTY.

    Q_PROPERTY(QVariantMap *myObject READ myObject WRITE setMyObject NOTIFY myObjectChanged)

    I guess I'm not sure where I should be using a pointer that I'm not.


  • Lifetime Qt Champion

    @Doug-Beck said in Casting a custom class to QVariant (2020 but still relevant):

    QVariant v = QVariant::fromValue<cMyClass>(MyObject);

    here



  • @Christian-Ehrlicher Still get the same errors even without that line of code.

    Start of compiler errors*
    error: C2280: 'cMyClass::cMyClass:(const cMyClass: &)': attempting to reference a deleted function
    compiler has generated 'cMyClass::cMyClass' here
    while compiling class template member function 'void *QtMetaTypePrivate::QMetaTypeFunctionHelper<T,true>::Construct(void *,const void *)'
    with
    [
    T=cMyClass
    ]

    see reference to function template instantiation 'void *QtMetaTypePrivate::QMetaTypeFunctionHelper<T,true>::Construct(void *,const void *)' being compiled
    with
    [
    T=cMyClass
    ]

    see reference to class template instantiation 'QtMetaTypePrivate::QMetaTypeFunctionHelper<T,true>' being compiled
    with
    [
    T=cFakeControl
    ]
    End of compiler errors*

    I sincerely appreciate your help on this! I have spent much of the last decade using C# so it would not be surprising if I didn't use a pointer correctly. But I'm not sure where the problem is.


  • Lifetime Qt Champion

    I don't know what else I can tell. You can't put a QObject into a QVariant, only a pointer to a QObject. Therefore pass a pointer ... c++ basics.



  • @Christian-Ehrlicher I get the part about pointers. However, when I copy cMyClass to an empty QML project and don't reference it at all and even remove all of the Q_PROPERTIES from cMyClass I still get the same error as before.

    This new project does not use QVariant at all. Nor does it ever refer to cMyClass, which is just an empty class. But, the line Q_DECLARE_METATYPE does cause compiler errors referring to a deleted function. (Which admittedly is just like I'd have if I was trying to use QObject instead of QObject*). So, this has nothing to do QVariant or even my use of cMyClass since I'm not using it at all.

    I hope you are saying that Q_DECLARE_METATYPE is completely unnecessary for the use of QVariant.

    #include <QObject>
    #include <QMetaType>

    class cMyClass: public QObject
    {
    Q_OBJECT
    public:
    explicit cMyClass(QObject* parent = nullptr);
    };

    Q_DECLARE_METATYPE(cMyClass);

    ** Still happens
    'cMyClass::cMyClass(const cMyClass &)': function was implicitly deleted because a base class invokes a deleted or inaccessible function 'QObject::QObject(const QObject &)'


  • Lifetime Qt Champion

    @Doug-Beck said in Casting a custom class to QVariant (2020 but still relevant):

    Q_DECLARE_METATYPE

    Hi
    https://doc.qt.io/qt-5/qmetatype.html#details
    "Any class or struct that has a public default constructor, a public copy constructor, and a public destructor can be registered."
    Which means you cant use
    Q_DECLARE_METATYPE(cMyClass);

    Also I think QObject * is already registered.



  • @mrjj Thanks! Clearly that was a dead end. I think I understand what Christian-Ehrlicher was telling me.


Log in to reply