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


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.


Reply
Contributor
Posts: 26
Registered: ‎07-24-2009
My Device: Not Specified
Accepted Solution

Set datamodel as context property

Code is:

theObject * robject = qobject_cast<theObject*>(qvariant_cast<QObject*>(dataModel->data(QVariantList() << i)));

	 for(int x = 0; x < robject->metaObject()->propertyCount(); ++x) {
	 context->setContextProperty(robject->metaObject()->property(x).name(), robject->metaObject()->property(x).read(robject));
	}

This method works for everything defined by default as a QVariant (for instance QStringList / QString etc...).

 

The definition of the property's is as follows:

 

    Q_PROPERTY(int count READ count CONSTANT)

    Q_PROPERTY(bb::cascades::DataModel* imageModel READ imageModel CONSTANT)
However that if I debug the value of metaObject()->property().read() then it returns the following:
Debug: count
Debug: QVariant(int, 4) 
Debug: imageModel 
Debug: QVariant(bb::cascades::DataModel*, ) 
If I check in QML I can access the count property but the imageModel property is null. I know for a fact that is isn't null. What have I done wrong?
Contributor
Posts: 26
Registered: ‎07-24-2009
My Device: Not Specified

Re: Set datamodel as context property

Anyone?

 

The data type is definitely registered as a QVariant as the property returns a type of QVariant::UserType.

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Set datamodel as context property

I'm no QVariant expert but are you sure that it is actually registered as a QVariant data defined type?

 

It could be any ancestor of datamodel that is returning user type?

 

Try registering it yourself and see what happens.

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Set datamodel as context property

I don't have time at the moment but do a search in the header files and see if it is?

If not search back through the ancestors.


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 1,178
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: Set datamodel as context property

DataModel is not a native Qt QVariant type. You could try putting this somewhere in your C++ code:

 

Q_DECLARE_METATYPE(bb::cascades::DataModel)

... but it probably won't work since that macro requires that the class to be registered has a public default constructor, a public copy constructor and a public destructor. No descendents of QObject meet this criteria, and although Cascades documentation doesn't make this clear, I believe DataModel is a QObject.

 

Rather than trying to pass stuff like this as an instance in a QVariant, you are better off using QVariant::fromValue() to put a copy of the pointer to the instance in the QVariant. This works even for unregistered types. Now you can pass the new QVariant to setContextProperty() and QML is smart enough to make it available as if you had registered the class directly.

 

I haven't tested if this is exactly the correct syntax in your situation but you could try something like this:

 

context->setContextProperty(robject->metaObject()->property(x).name(), QVariant::fromValue(robject->metaObject()->property(x).read(robject)));

cheechm wrote:

Code is:

theObject * robject = qobject_cast<theObject*>(qvariant_cast<QObject*>(dataModel->data(QVariantList() << i)));

	 for(int x = 0; x < robject->metaObject()->propertyCount(); ++x) {
	 context->setContextProperty(robject->metaObject()->property(x).name(), robject->metaObject()->property(x).read(robject));
	}

This method works for everything defined by default as a QVariant (for instance QStringList / QString etc...).

 

The definition of the property's is as follows:

 

    Q_PROPERTY(int count READ count CONSTANT)

    Q_PROPERTY(bb::cascades::DataModel* imageModel READ imageModel CONSTANT)
However that if I debug the value of metaObject()->property().read() then it returns the following:
Debug: count
Debug: QVariant(int, 4) 
Debug: imageModel 
Debug: QVariant(bb::cascades::DataModel*, ) 
If I check in QML I can access the count property but the imageModel property is null. I know for a fact that is isn't null. What have I done wrong?



 



Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.  multiFEED Icon

Play nice: Clicking Like Button on posts that helped you not only encourages others to continue sharing their experience, but also improves your own rating on this board. Also, don't forget to accept a post if it solves your problem or answers your question.
Developer
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: Set datamodel as context property

Isn't it usually the case that you dont update the context property after the qml document is created?

I use bb::cascades::ArrayDataModel as a Q_PROPERTY, maybe try declaring the property with one of the child classes of DataModel?

What is the intent of the code, this seems intense
Like all of my posts
Contributor
Posts: 26
Registered: ‎07-24-2009
My Device: Not Specified

Re: Set datamodel as context property

Having now taken my mind off it for a while and returned I realise what I was doing was completly ridiculous. 

 

I was loosely following https://github.com/blackberry/Cascades-Samples/tree/master/repeater

 

What I was doing was iterating through all of the properties and setting them seperately. However the easiest way to do it is just expose the object to the QML thereby exposing all of the properties.

 

So all I needed to do was:

 

context->setContextProperty("RepeaterData", robject);

Then in QML I can access each property as RepeaterData.*** etc..

 

Cheers for the help guys. Sorted!