[Interest] QVariant cannot take custom type

Samuel Gaist samuel.gaist at edeltech.ch
Fri Apr 5 09:50:23 CEST 2013


Hi,

IIRC, you can't have Item or Holder in a QVariant because there QObject so the copy constructor is disabled for them.

This thread might be of interest http://stackoverflow.com/questions/7872578/how-to-properly-use-qregistermetatype-on-a-class-derived-from-qobject

Hope it helps

On 5 avr. 2013, at 09:37, Aekold Helbrass wrote:

> Hi All!
> 
> Short story is: I'm trying to put custom type into QVariant and it doesn't work, on debug I see QVariant with value <not accessible> and trying to read it I'm getting segfault.
> 
> Long story, I'm new to Qt/C++ so I'm trying to do everything very carefully, but I'm almost sure it's some rookie mistake in my code. This code started as attempt to bind JSON tree to object tree, and stopped on reading of QList, because QMetaProperty wants QVariant.
> 
> Source code:
> ================ main.cpp ====================
> #include <QCoreApplication>
> #include <QObject>
> #include <QList>
> #include <QMetaType>
> #include <QMetaObject>
> #include <QMetaProperty>
> #include <QDebug>
> 
> #include "item.h"
> #include "holder.h"
> 
> Q_DECLARE_METATYPE(Item)
> Q_DECLARE_METATYPE(Item*)
> Q_DECLARE_METATYPE(Holder)
> 
> int main(int argc, char *argv[]) {
>     QCoreApplication a(argc, argv);
> 
>     int typeItem = qRegisterMetaType<Item>("Item");
>     int typeHolder = qRegisterMetaType<Holder>("Holder");
>     int typeItemList = qRegisterMetaType<QList<Item*> >("QList<Item*>>");
>     int typeItemPointer = qRegisterMetaType<Item*>("Item*");
> 
>     QList<QVariant> list;
>     for (int i = 0; i < 10; i++) {
>         void* created = QMetaType::create(typeItem);
>         Item* item = static_cast<Item*>(created);
>         item->setName("item " + i);
> // attempt 1:
>         QVariant var(typeItem, created);
> // attempt 2:
>         //QVariant var;
>         //var.setValue(item);
> // attempt 3:
>         //QVariant var = QVariant::fromValue(item);
>         list.append(var);
>     }
> 
>     Holder* holder = new Holder();
>     int index = holder->metaObject()->indexOfProperty("items");
>     QMetaProperty property = holder->metaObject()->property(index);
>     property.write(holder, list);
> 
>     QList<Item*> items = holder->items();
>     for (int i = 0; i < items.size(); i++) {
>         Item* item = items.at(i);
>         qDebug() << "name: " << item->name();
>     }
> 
>     return a.exec();
> }
> 
> ======================================= holder.h ===============================
> #ifndef HOLDER_H
> #define HOLDER_H
> 
> 
> #include <QObject>
> 
> #include <QList>
> #include "item.h"
> 
> class Holder : public QObject {
> 
>     Q_OBJECT
>     Q_PROPERTY(QList<Item*> items READ items WRITE setItems)
> 
> public:
>     Holder(QObject* parent=0) : QObject(parent) {}
> 
>     Holder(const Holder& copy, QObject* parent=0) : QObject(parent) {
> 
>         pItems = copy.items();
> 
>     }
>     Holder& operator =(const Holder& value) {
> 
>         pItems = value.items();
> 
>     }
>     QList<Item*> items() const {
> 
>         return pItems;
> 
>     }
> public slots:
> 
>     void setItems(const QList<Item*>& value) {
> 
>         pItems = value;
> 
>     }
> private:
> 
>     QList<Item*> pItems;
> 
> };
> 
> #endif // HOLDER_H
> 
> 
> 
> ============================== item.h ================================
> #ifndef ITEM_H
> 
> #define ITEM_H
> 
> #include <QObject>
> 
> class Item : public QObject {
> 
>     Q_OBJECT
>     Q_PROPERTY(QString name READ name WRITE setName)
> 
> public:
>     Item(QObject* parent=0) : QObject(parent) {}
> 
>     Item(const Item& copy, QObject* parent=0) : QObject(parent) {
> 
>         pName = copy.name();
> 
>     }
>     Item& operator =(const Item& value) {
> 
>         pName = value.name();
> 
>     }
>     QString name() const {
> 
>         return pName;
> 
>     }
> 
> public slots:
> 
>     void setName(const QString& value) {
> 
>         pName = value;
> 
>     }
> private:
> 
>     QString pName;
> 
> };
> 
> #endif // ITEM_H
> 
> 
> 
> ======================== end ===================================
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest




More information about the Interest mailing list