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

Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.

Posts: 13
Registered: ‎11-30-2012
My Device: Alpha
My Carrier: Pepephone
Accepted Solution

GroupDataModel item cast to custom object

I have a GroupDataModel (mContactsModel) of a listView made by custom objects BBMContact.


On list triggered, I want to get the BBMContact selected on the listView.


If I try


BBMContact contact_aux = qvariant_cast<BBMContact>(mContactsModel->data(indexPath))

 I get


/qnx6/usr/include/qt4/QtCore/qmetatype.h:169:80: error: 'qt_metatype_id' is not a member of 'QMetaTypeId<BBMContact>'


How can I get the selected BBMContact object?


Posts: 43
Registered: ‎12-16-2011
My Device: Torch 9800 & Playbook
My Carrier: Three

Re: GroupDataModel item cast to custom object

[ Edited ]



To do this you have two options I can think of:


1, register the type using Q_DECLARE_METATYPE() & qRegisterMetaType().





Qobject *obj = mContantsModel->data(indexPath).value<QObject *>();
BBMContact *contact_aux = qobject_cast<BBMContact *>(obj);



Daniel - Neurocode
If my posts help, please click the Like button.

gymChronicles - helping you to keep motivated
Posts: 13
Registered: ‎11-30-2012
My Device: Alpha
My Carrier: Pepephone

Re: GroupDataModel item cast to custom object

Second option works perfect. Thanks a lot!!!

Posts: 19
Registered: ‎05-09-2013
My Device: Z10
My Carrier: None

Re: GroupDataModel item cast to custom object

[ Edited ]

I have a similar Issue. I keep custom objects that derive from QObject inside a GroupDataModel.

My type is registered via the QT_DECLARE_METATYPE macro and qRegisterMetaType method.


I have a onTriggered handler inside a context menu ActionItem that lives within a ListView. I want to call a Q_INVOKABLE method passing it the selected object.




onTriggered {






Q_INVOKABLE void addToFavourites(const QVariant &item) {

      MyItem * item = static_cast<MyItem*>(item.value<QObject*>()); // this casting seems extremely ugly.

      // do something with my item...



What I wonder, is there a more elegant way? I Thought I could change the parameter type from 'const QVariant &item' to 'MyItem &item' to avoid casting, but QML complains that MyItem& type is unknown to it ('unknown method parameter type MyItem&');


Neurocode mentioned 2 options, but I am not sure how option 1) is supposed to work.


Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: GroupDataModel item cast to custom object

[ Edited ]

GroupDataModel's data() method returns QVariant, so I think conversion is unavoidable unless you implement a custom data model with method returning a strictly typed item and use this method in QML.


This line:

MyItem * item = static_cast<MyItem*>(item.value<QObject*>());

can be simplified to:

MyItem *item = item.value<MyItem *>();

This should work if metatype is declared & registered. I think it was the first option mentioned above.

Andrey Fidrya, @zmeyc on twitter