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
Highlighted
Developer
Posts: 188
Registered: ‎04-07-2013
My Device: BlackBerry Z10
My Carrier: Telus
Accepted Solution

Add a QML Control from C++

//MyListView.qml
import bb.cascades 1.0 ListView { id: myListView horizontalAlignment: HorizontalAlignment.Fill verticalAlignment: VerticalAlignment.Fill listItemComponents: [ ListItemComponent { type: "item" CategoryItem { title: ListItemData.categoryIcon description: ListItemData.categoryName } } ] }

 Hi all, the above code is MyListView.qml.

 

I would like to add it to my Container from C++, any sample code on that please?

 

 

jayvdiyk.com
Developer
Posts: 188
Registered: ‎04-07-2013
My Device: BlackBerry Z10
My Carrier: Telus

Re: Add a QML Control from C++

Anyone please? Thank you

jayvdiyk.com
Developer
Posts: 196
Registered: ‎03-04-2013
My Device: BB Z10
My Carrier: Rogers

Re: Add a QML Control from C++

Well, the first thing is that you could just create the ListView with C++ coding instead of having it in a QML file.  In which case you would have to use ListItemProvider and not ListItemComponent...

https://developer.blackberry.com/cascades/reference/bb__cascades__listitemprovider.html

 

Having said that you could try:

1) You can try this code first, if it doesn't work you may need to add Page { } in your QML file and place the ListView inside...

2) Somewhere in your C++ code, it could be the class constructor function but you'd want your ListView to be a private member variable in that case, so that it doesn't go out of scope etc etc...

    QmlDocument *qml = QmlDocument::create("asset:///MyListView.qml").parent(app);

    // create root object for the UI
    ListView *myListView = qml->createRootObject<ListView>();

...
...
    myContainer->add(myListView);

 

And if that doesn't work with the createRootObject and the ListView, try:

1) Give your ListView an objectName: "myListView" ...

2) Put your ListView in a Page { }

 

//MyListView.qml
import bb.cascades 1.0

Page {
    ListView {
        id: myListView
        objectName: "myListView"
        horizontalAlignment: HorizontalAlignment.Fill
        verticalAlignment: VerticalAlignment.Fill
    
        listItemComponents: [
            ListItemComponent {
                type: "item"
                CategoryItem {
                    title: ListItemData.categoryIcon
                    description: ListItemData.categoryName
                }
            }
        ]
    }
}

 

3)

 

    QmlDocument *qml = QmlDocument::create("asset:///MyListView.qml").parent(app);

    // create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();

    ListView *myListView = root->findChild<ListView*>("myListView");

 

Developer
Posts: 206
Registered: ‎05-15-2012
My Device: None
My Carrier: Telus

Re: Add a QML Control from C++

Yep, as stated you'll need to extend ListItemProvider. This is a substantial amount of work, so unless you're planning on building a highly customized, very powerful, list, I wouldn't bother. If you do want to go down that route, as jalue stated, check out the listitemprovider documentation. It's quite thorough.

----------------------
Check out my app, Alien Flow for reddit

And of course, like my post if you found it helpful or informative!