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)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 2.8k 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
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • D Doug Beck

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

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

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

        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
        4
        • Christian EhrlicherC Christian Ehrlicher

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

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

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

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            3
            • D Offline
              D Offline
              Doug Beck
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • 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 Online
                      Christian EhrlicherC Online
                      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 Online
                          Christian EhrlicherC Online
                          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 Online
                              Christian EhrlicherC Online
                              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