QVariant custom type returning QVariant::QPaintBufferCacheEntry
-
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,
-
Hi,
// qRegisterMetaType<MyClass >("MyClass1"); // qRegisterMetaType<MyClass >("MyClass2");
Looks like you are calling qRegisterMetaType twice for the same class
-
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
-
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 destructorHere 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,
-
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