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
Contributor
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?

 

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

Re: GroupDataModel item cast to custom object

[ Edited ]

Hi,

 

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

 

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

 

or

 

2,

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

 

regards

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

gymChronicles - helping you to keep motivated
Contributor
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!!!

Contributor
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.

 

QML:

 

onTriggered {

   addToFavourites(ListItemData);

}

 

C++:

 

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.

 

Developer
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