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: 16
Registered: ‎01-17-2013
My Device: None
My Carrier: None
Accepted Solution

Adding options to a DropDown from c++

In my C++ files I have a QMap of data that I would like to add to a DropDown that I have in QML.

 

I can't find any examples of how to add options to an already existing DropDown though.  Here's what I have in my QML file right now:

DropDown {
                    id: account
                    title : "Account"
                    enabled : true
                }

 It seems like it should be a fairly easy process to get the DropDown list by the id and insert data from my QMap into it.

 

Any help with this would be greatly appreciated!

Contributor
Posts: 43
Registered: ‎01-05-2013
My Device: Dev Alpha
My Carrier: Telenor Hungary

Re: Adding options to a DropDown from c++

Hi,

in the class reference is a C++ example:
https://developer.blackberry.com/cascades/reference/bb__cascades__dropdown.html

If you need more help, there is a sample application in github, where every UI controller is presented in C++:

https://github.com/blackberry/Cascades-Samples/tree/master/cascadescookbookcpp
--------------------------------------------------------------
If my post was helpful or it was the solution for Your problem, please don't forget to give me a like, and mark my post as a solution. Thanks!
Contributor
Posts: 16
Registered: ‎01-17-2013
My Device: None
My Carrier: None

Re: Adding options to a DropDown from c++

The class reference it isn't populating an existing DropDown. It's creating a new one entirely in C++.

I don't want to build the entire QML structure in C++, I'd rather write the QML code out and then populate the DropDown with data after the fact.

I haven't come across any easy way to do this though.
Highlighted
Contributor
Posts: 43
Registered: ‎01-05-2013
My Device: Dev Alpha
My Carrier: Telenor Hungary

Re: Adding options to a DropDown from c++

Oh, I did not see, that you don't know how to connect the C++ logic to the QML UI, The QML component ID is not enough, you must define objectName: "dropDown" property too.

You can reach any QML object from cpp like this:

//this snippet is from YourProjectName.cpp
// create scene document from main.qml asset
// set parent to created document to ensure it exists for the whole
// application lifetime

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

// create root object for the UI
AbstractPane *root = qml->createRootObject<AbstractPane>();
// set created root object as a scene
app->setScene(root);

//and here comes the findChild method
DropDown* dpList = root->findChild<DropDown*>("dropDown");

After this line, dpList will point to the UI controller.
--------------------------------------------------------------
If my post was helpful or it was the solution for Your problem, please don't forget to give me a like, and mark my post as a solution. Thanks!
Contributor
Posts: 16
Registered: ‎01-17-2013
My Device: None
My Carrier: None

Re: Adding options to a DropDown from c++

I marked your previous message as the Solution but I'm actually still having problems. Apparently you can't use findChild on an AbstractPane.

I get the following error:
no matching function for call to 'bb::cascades::AbstractPane::findChild(const char [9])'
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Adding options to a DropDown from c++

[ Edited ]

Hi,

Can you please show how do you call findChild? It's a template function. The following forms should work:

AbstractPane *root = qml->createRootObject<AbstractPane>();

QObject *button = root->findChild<QObject *>("button");
or
Container *container = root->findChild<Container *>("rootContainer");

 

Is the following line present in .cpp?

 

#include <bb/cascades/AbstractPane>

 

UPD:

I've checked that this function is actually defined in QObject and AbstractPane inherits QObject. I was able to compile the following code:

#include <bb/cascades/AbstractPane>
...
bb::cascades::AbstractPane *pane;
pane->findChild<QObject *>("test");

 


Andrey Fidrya, @zmeyc on twitter
Contributor
Posts: 16
Registered: ‎01-17-2013
My Device: None
My Carrier: None

Re: Adding options to a DropDown from c++

Zmey, It works with QObject but there's no add() function on a QObject to add items to the DropDown... which is the whole point of this.

Maybe I'm going about this wrong, but all I want is to get a DropDown populated with dynamic data that I can't hardcode into it. It seems like this should be a very trivial task.

If there's a better way to do this, please let me know.
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Adding options to a DropDown from c++

[ Edited ]

This should work I think:

 

#include <bb/cascades/AbstractPane>
#include <bb/cascades/DropDown>

using namespace bb::cascades;

...

DropDown *dropDown = pane->findChild<DropDown *>("account");
dropDown->add(Option::create().text("Option 1"));

 


Andrey Fidrya, @zmeyc on twitter