Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Zmey
Posts: 1,511
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: How to read data out of a QVariant

[ Edited ]

@Ekke

 

This approach would work for a list of any QObject-derived classes, but it's a list of QVariants.

 

qobject_cast performs a lookup on runtime type information managed by QObject. It won't work with QVariant because it's not QObject-derived, plus, we're extracting it's value and not converting the QVariant itself.

 

 

I found that there's qvariant_cast function which seems to be equivalent of calling .value<>() on QVariant. I've made a test project to test various methods and qobject_cast didn't compile:

 

TestClass *c = new TestClass;
qDebug() << "c" << c;

QVariant v = QVariant::fromValue(c);
qDebug() << "v" << c;

// This produces compilation error:
// no matching function to call to 'qobject_cast'. No known conversion from 'QVariant' to 'QObject *'
//TestClass *c2 = qobject_cast<TestClass *>(v);
//qDebug() << "c2" << c2;

TestClass *c3 = v.value<TestClass *>();
qDebug() << "c3" << c3;

TestClass *c4 = qvariant_cast<TestClass *>(v);
qDebug() << "c4" << c4;

 

The output:

c TestClass(0x101e120e0) 
v TestClass(0x101e120e0) 
c3 TestClass(0x101e120e0) 
c4 TestClass(0x101e120e0) 

Tested this on Qt 5.x, but behavior on 4.8 should be the same. The only difference in Qt 5 is that QObject-derived classes no longer require Q_DECLARE_METATYPE to be used in QVariants.

 

@qbalsdon,

 

Another idea on why it's not working: please show how do you populate the model with objects. If they were casted to QObject*, then you should extract them as QObject* too and cast them to appropriate class using qobject_cast. There was no Q_DECLARE_METATYPE macro in header so it's likely that there was some kind of workaround when adding them to the model.

 

upd:

QVariant(QObject*, Person(0x857c310) )

 ^^^ logs show that they were indeed stored as QObject pointers.

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
ekke
Posts: 1,460
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: How to read data out of a QVariant


Zmey wrote:

@Ekke

 

This approach would work for a list of any QObject-derived classes, but it's a list of QVariants.

 

qobject_cast performs a lookup on runtime type information managed by QObject. It won't work with QVariant because it's not QObject-derived, plus, we're extracting it's value and not converting the QVariant itself.

 

 

.....

 


You're right - I'm dealing with List of QObject* in my use-cases

have overlooked that problem in this case is using QVariants

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

Open Source Enthusiast
blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner
bb10-development: http://appbus.org Twitter: @ekkescorner
Please use plain text.
New Contributor
qbalsdon
Posts: 9
Registered: ‎06-15-2013
My Device: Emulator
My Carrier: MTN

Re: How to read data out of a QVariant

For some reason, I have to cast it out of the list as a QObject* and then cast it to a Person* after it's out of the deata model. Very weird, but it works.

Please use plain text.