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. QVariant custom type returning QVariant::QPaintBufferCacheEntry
Qt 6.11 is out! See what's new in the release blog

QVariant custom type returning QVariant::QPaintBufferCacheEntry

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.1k 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.
  • E Offline
    E Offline
    Erakis
    wrote on last edited by Erakis
    #1

    Hi,

    I'm having problem getting custom type name from a QVariant using a QtQuick project on Qt 5.4.1

    class MyClass : public QObject
    {
        Q_OBJECT
    }
    Q_DECLARE_METATYPE(MyClass)
    
    class MyClass2 : public QObject
    {
        Q_OBJECT
    }
    Q_DECLARE_METATYPE(MyClass2)
    

    Now in the main.cpp :

    class MyClass1 : public QObject
    {
        Q_OBJECT
    }
    Q_DECLARE_METATYPE(MyClass1)
     
    class MyClass2 : public QObject
    {
        Q_OBJECT
    }
    Q_DECLARE_METATYPE(MyClass2)
     
    // Now in the main.cpp :
    MyClass1 v1;
    QVariant t1 = QVariant::fromValue(v1);
    MyClass2 v2;
    QVariant t2 = QVariant::fromValue(v2);
     
    qDebug() << t1.type();  // Return QVariant::QPaintBufferCacheEntry
    qDebug() << t2.type();  // Return QVariant::QPaintBufferCacheEntry
     
    qDebug() << t1.userType();  // Return 1060
    qDebug() << t2.userType();  // Return 1060
     
    qDebug() << qMetaTypeId<MyClass1>();  // Return 1060
    qDebug() << qMetaTypeId<MyClass2>();  // Return 1061
    

    Now, how can I compare if t1 is the same type as t2 if both are returning the same type ID ?

    It is a bug ? If yes, then is there a workaround to get the correct type ? I want to put element of "MyClass" and "MyClass" inside a QVariantList and while browsing the list do specific work according the type of the object.

    The only workaround I found for now is having a base class "MyClassBase". Next derivate "MyClass" and "MyClass2" from the "MyClassBase". The "MyClassBase" could have a virtual method GetCustomType(). But I'm a bit disappointed that QT metatype does not seem to work here.

    Best regards,

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      // qRegisterMetaType<MyClass >("MyClass1");
      // qRegisterMetaType<MyClass >("MyClass2");
      

      Looks like you are calling qRegisterMetaType twice for the same class

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Erakis
        wrote on last edited by
        #3

        Yes but I forget to remove this code before pasting. Sorry. Moreover it is not part of the question. I remove it from the original question

        1 Reply Last reply
        0
        • E Offline
          E Offline
          Erakis
          wrote on last edited by Erakis
          #4

          Hi,

          I create a class derivated from QAbstractListModel and I need to put it into a QVariantList. Forcing me to use the Q_DECLARE_METATYPE macro as if I well understand everything we put inside a QVariant need to be declared with Q_DECLARE_METATYPE.

          But I read on QT documentation that "All Qt classes are noncopyable by deriving from QObject.". Obviously QAbstractListModel derived from QObject !

          So I'm in the obligation to provide 3 things :
          1 - Public default constructor
          2 - Public copy constructor
          3 - Public destructor

          Here is my implementation :

          class CVariable : public QObject
          {
              Q_OBJECT
              Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
          
          public:
              explicit CVariable(QObject *parent = 0);
              explicit CVariable(const QString& text, QObject *parent = 0);
              CVariable(const CVariable& copy);
              ~CVariable();
          
          public:
              QString text() const;
          
          signals:
               void textChanged(const QString& newText);
          
          public slots:
              void setText(const QString& text);
          
          private:
              QString  m_text;
          };
          Q_DECLARE_METATYPE(CVariable)
          
          
          class CVariableList : public QAbstractListModel
          {
              Q_OBJECT
              Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
          
          public:
              enum InterfaceVariableListModelDataRole
              {
                  TextRole = Qt::UserRole + 1
              };
          
          public:
              CVariableList(QObject *parent = 0);
              // Basing on QT documentation I would need to declare a copy constructor here to get thing work with Q_DECLARE_METATYPE.
          
              int rowCount(const QModelIndex &parent = QModelIndex()) const;
          
              QVariant data(const QModelIndex &index, int role = TextRole) const;
          
              void addVariable(CVariable* pVariableInfo);
          
              CVariable* getVariable(int iRow);
          
              void issueRowCountChangeEmit(int iRow);
          
          protected:
              QHash<int, QByteArray> roleNames() const;
          
          signals:
              void countChanged(int iNewCount);
          
          private:
              QList<CVariable*>      m_VariableList;
          };
          
          Q_DECLARE_METATYPE(CVariableList)
          

          I have to keep a list that can contain either a CVariable* or CVariableList*. The only way I found is to use a QVariantList. However while browsing this list I have to distinguish the element (CVariable* or CVariableList*). I was counting on the help of :

          for ( QVariantList::const_iterator it = list.begin(); it!=list.end(); ++it )
          {
                 if ( qMetaTypeId<CVariable *> == i>userType() )
                {
                        ....
                }
          }
          

          But unfortunately

          QVariant::fromValue(&myVariable).userType() 
          

          is returning the same meta ID as

          QVariant::fromValue(&myVariableList).userType() 
          

          It is a bug or there something I do not understand ?

          Best regards,

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

            Both are pointers to QObject that's why you get the same ID.

            Note that what you are currently doing is wrong. QObject is not copyable

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            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