Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Casting a custom class to QVariant (2020 but still relevant)

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

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 3.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Doug Beck
    wrote on last edited by
    #7

    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.

    mrjjM 1 Reply Last reply
    0
    • D Doug Beck

      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.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #8

      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

      D 1 Reply Last reply
      0
      • mrjjM mrjj

        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

        D Offline
        D Offline
        Doug Beck
        wrote on last edited by
        #9

        @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.

        Christian EhrlicherC 1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #10

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

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          D 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

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

            D Offline
            D Offline
            Doug Beck
            wrote on last edited by
            #11

            @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.

            1 Reply Last reply
            0
            • D Doug Beck

              @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.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #12

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

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

              here

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              D 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

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

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

                here

                D Offline
                D Offline
                Doug Beck
                wrote on last edited by
                #13

                @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.

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #14

                  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.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  D 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    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.

                    D Offline
                    D Offline
                    Doug Beck
                    wrote on last edited by Doug Beck
                    #15

                    @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 &)'

                    mrjjM 1 Reply Last reply
                    0
                    • D Doug Beck

                      @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 &)'

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #16

                      @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.

                      D 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        @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.

                        D Offline
                        D Offline
                        Doug Beck
                        wrote on last edited by
                        #17

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

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved