QObjects are not supposed to be copiable, providing a copy constructor violates Qt's principle that QObject is an identity type. If you want to store QObjects in QVariant store pointers to be them instead. ________________________________ From: interest-bounces+pritam_ghanghas=infosys....@qt-project.org [interest-bounces+pritam_ghanghas=infosys....@qt-project.org] on behalf of Aekold Helbrass [helbr...@gmail.com] Sent: Friday, April 05, 2013 1:31 PM To: Samuel Gaist Cc: Qt-interest Interest Subject: Re: [Interest] QVariant cannot take custom type
I've provided copy-constructor, as was suggested in that article. On Fri, Apr 5, 2013 at 10:50 AM, Samuel Gaist <samuel.ga...@edeltech.ch<mailto:samuel.ga...@edeltech.ch>> wrote: 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<http://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<http://copy.name>(); > > } > Item& operator =(const Item& value) { > > pName = value.name<http://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@qt-project.org<mailto:Interest@qt-project.org> > http://lists.qt-project.org/mailman/listinfo/interest _______________________________________________ Interest mailing list Interest@qt-project.org<mailto:Interest@qt-project.org> http://lists.qt-project.org/mailman/listinfo/interest **************** CAUTION - Disclaimer ***************** This e-mail contains PRIVILEGED AND CONFIDENTIAL INFORMATION intended solely for the use of the addressee(s). If you are not the intended recipient, please notify the sender by e-mail and delete the original message. Further, you are not to copy, disclose, or distribute this e-mail or its contents to any other person and any such actions are unlawful. This e-mail may contain viruses. Infosys has taken every reasonable precaution to minimize this risk, but is not liable for any damage you may sustain as a result of any virus in this e-mail. You should carry out your own virus checks before opening the e-mail or attachment. Infosys reserves the right to monitor and review the content of all messages sent to or from this e-mail address. Messages sent to or from this e-mail address may be stored on the Infosys e-mail system. ***INFOSYS******** End of Disclaimer ********INFOSYS*** _______________________________________________ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest