working main.cpp is attached. I am not sure why you want to do this. ________________________________ From: Aekold Helbrass [helbr...@gmail.com] Sent: Friday, April 05, 2013 5:59 PM To: Pritam Ghanghas Cc: Qt-interest Interest Subject: Re: [Interest] QVariant cannot take custom type
Complete QtCreator project in attachment. I'm running it on QtCreator 2.7.0 and Qt 5.0.1, SUSE Linux 12.3. On Fri, Apr 5, 2013 at 2:30 PM, Pritam Ghanghas <pritam_ghang...@infosys.com<mailto:pritam_ghang...@infosys.com>> wrote: can you send compilable code that reproduces this problem. I will try here. ________________________________ From: interest-bounces+pritam_ghanghas=infosys....@qt-project.org<mailto:infosys....@qt-project.org> [interest-bounces+pritam_ghanghas=infosys....@qt-project.org<mailto:infosys....@qt-project.org>] on behalf of Aekold Helbrass [helbr...@gmail.com<mailto:helbr...@gmail.com>] Sent: Friday, April 05, 2013 4:45 PM To: Qt-interest Interest Subject: Re: [Interest] QVariant cannot take custom type Ok, I've tried this one: QVariant var(QMetaType::QObjectStar, &item); and it almost works. Pointer somehow gets crippled after using QMetaProperty.write. When Item* is created originally - address is 0x1b952fo. But into Holder it is written as 0x1b95480. Is is even possible? Maybe I'm constructing QMetaProperty not as intended?... On Fri, Apr 5, 2013 at 2:00 PM, Pritam Ghanghas <pritam_ghang...@infosys.com<mailto:pritam_ghang...@infosys.com><mailto:pritam_ghang...@infosys.com<mailto:pritam_ghang...@infosys.com>>> wrote: use gdb and see where exactly it segfaults. That will give better pointers than random guesses. You can try posting backtrace here. ________________________________ From: interest-bounces+pritam_ghanghas=infosys....@qt-project.org<mailto:infosys....@qt-project.org><mailto:infosys....@qt-project.org<mailto:infosys....@qt-project.org>> [interest-bounces+pritam_ghanghas=infosys....@qt-project.org<mailto:infosys....@qt-project.org><mailto:infosys....@qt-project.org<mailto:infosys....@qt-project.org>>] on behalf of Aekold Helbrass [helbr...@gmail.com<mailto:helbr...@gmail.com><mailto:helbr...@gmail.com<mailto:helbr...@gmail.com>>] Sent: Friday, April 05, 2013 4:15 PM To: Conti Manuele Cc: Qt-interest Interest Subject: Re: [Interest] QVariant cannot take custom type Thanx, that one was close, but still segfault: QSharedPointer<Item> pointer(item); QVariant var = QVariant::fromValue(pointer); list.append(var); On Fri, Apr 5, 2013 at 1:33 PM, Conti Manuele <conti...@alice.it<mailto:conti...@alice.it><mailto:conti...@alice.it<mailto:conti...@alice.it>><mailto:conti...@alice.it<mailto:conti...@alice.it><mailto:conti...@alice.it<mailto:conti...@alice.it>>>> wrote: Hi All, There is a problem in your code, actually I use this and work perfectly: Q_DECLARE_METATYPE(QSharedPointer<QPlcServerSocket::QExternalCommand>) qRegisterMetaType< QSharedPointer<QPlcServerSocket::QExternalCommand> >(); Bye On 04/05/2013 12:27 PM, Samuel Gaist wrote: > He explains how to handle QObject * so a pointer to QObject instance, not a > QObject instance. > > On 5 avr. 2013, at 12:22, Aekold Helbrass wrote: > >> So, it is absolutely impossible to use QObject-derived class with QVariant? >> I'm storing QList of pointers already, and I want to write that with >> QMetaProperty. The only thing that needs copy-constructor, in my >> understanding, is qRegisterMetaType invocation. >> >> I even read an article where guy explains how to get other types than one >> you're storing, so it looks like there is no problem in storing QObject in >> QVariant, but for some reason it doesn't work for me: >> http://steveire.wordpress.com/2011/03/16/implementing-qvariantqmetatype-features-with-template-tricks/ >> >> >> On Fri, Apr 5, 2013 at 12:44 PM, André Somers >> <an...@familiesomers.nl<mailto:an...@familiesomers.nl><mailto:an...@familiesomers.nl<mailto:an...@familiesomers.nl>><mailto:an...@familiesomers.nl<mailto:an...@familiesomers.nl><mailto:an...@familiesomers.nl<mailto:an...@familiesomers.nl>>>> >> wrote: >> Op 5-4-2013 11:39, Sven Bergner schreef: >>> Hello, >>> you can use a custom type with QVariant by declaring it >>> using Q_DECLARE_METATYPE ( Type ). >>> Hope that helps. >>> >> It does not. That was already in the code posted in the opening post. As >> noted by others, the issue lies with trying to use a QObject derived >> class here. >> >> André >> >> -- >> You like Qt? >> I am looking for collegues to join me at i-Optics! >> >> _______________________________________________ >> Interest mailing list >> Interest@qt-project.org<mailto:Interest@qt-project.org><mailto:Interest@qt-project.org<mailto:Interest@qt-project.org>><mailto:Interest@qt-project.org<mailto:Interest@qt-project.org><mailto: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><mailto:Interest@qt-project.org<mailto:Interest@qt-project.org>><mailto:Interest@qt-project.org<mailto:Interest@qt-project.org><mailto: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><mailto:Interest@qt-project.org<mailto:Interest@qt-project.org>><mailto:Interest@qt-project.org<mailto:Interest@qt-project.org><mailto: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><mailto:Interest@qt-project.org<mailto:Interest@qt-project.org>><mailto:Interest@qt-project.org<mailto:Interest@qt-project.org><mailto: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***
#include <QCoreApplication> #include <QObject> #include <QList> #include <QMetaType> #include <QMetaObject> #include <QMetaProperty> #include <QDebug> #include <QSharedPointer> #include <QVariant> #include "item.h" #include "holder.h" Q_DECLARE_METATYPE(Item) Q_DECLARE_METATYPE(Item*) Q_DECLARE_METATYPE(Holder) Q_DECLARE_METATYPE(QList<Item*>) Q_DECLARE_METATYPE(QSharedPointer<Item>) 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*"); int typePointer = qRegisterMetaType<QSharedPointer<Item*> >("QSharedPointer<Item>"); QList<Item*> list; for (int i = 0; i < 10; i++) { void* created = QMetaType::create(typeItem); Item* item = static_cast<Item*>(created); item->setName("item " + QString::number(i)); qDebug() << item->name(); // attempt 1: //QVariant var(typeItem, created); // attempt 2: //QVariant var; //var.setValue(item); // attempt 3: //QVariant var = QVariant::fromValue(item); //QVariant var = qVariantFromValue((void*)created); //void* extracted = var.value<void*>(); //Item* it2 = var.value<Item*>(); //QSharedPointer<Item> pointer(item); //QVariant var = QVariant::fromValue(pointer); // QVariant var(QMetaType::QObjectStar, item); // Item* it2 = var.value<Item*>(); //QVariant var(QMetaType::VoidStar, &created); list.append(item); } Holder* holder = new Holder(); int index = holder->metaObject()->indexOfProperty("items"); QMetaProperty property = holder->metaObject()->property(index); QVariant var = QVariant::fromValue(list); property.write(holder, var); 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(); }
_______________________________________________ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest