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
New Contributor
sinkpoint
Posts: 3
Registered: ‎02-10-2013
My Device: none
Accepted Solution

Anyway to have an onLoad function in qml when adding to NavigationPane?

I'm new to Blackberry 10 dev. So I'm wondering what's the best way to do this as I'm not getting any clear answers from the dev docs.

What I want is to start a separate view in my app from a navigation screen. The new page will then create a http request and update the UI based on the output.

The best way seems to be using the NavigationPane and add a qml view. However how do I invoke a C++ function when it's pushed onto the stack? Something similar to android onActivityCreated() in Fragments. There is the Http example docs, but the program started the http request from the constructor of the inherited QObject. How to I have a function executed as the new qml is added to the navigation stack as

 

 

 // navigationpane.qml

NavigationPane {
    id: navigationPane
    Page {
        Container {
            Label { 
                text: "First page"
            }
        }

        actions: [
            ActionItem {
                title: "Next page"
                ActionBar.placement: ActionBarPlacement.OnBar
                onTriggered: {
                    var page = pageDefinition.createObject();
                    navigationPane.push(page);
                }

                attachedObjects: ComponentDefinition {
                    id: pageDefinition;
                    source: "secondpage.qml"
                }
            }
        ]
    }
    onPopTransitionEnded: { page.destroy(); }
}
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: Anyway to have an onLoad function in qml when adding to NavigationPane?

Most QML objects have a creationCompleted signal, so you can define an onCreationCompleted: { ... } to do what I believe you're asking for.

Note that it executes only when the thing is first created though... if you're reusing it (hiding but not destroying, then later making visible again) you'll need something else. Some components have relevant signals for that (e.g. onOpened, I think, for Dialog) but for others you'll need to make your own signal (which is trivial to do though).

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
New Contributor
sinkpoint
Posts: 3
Registered: ‎02-10-2013
My Device: none

Re: Anyway to have an onLoad function in qml when adding to NavigationPane?

Thanks for the quick reponse.

A related question is that:

In android, an app can have multiple activities and/or fragments. Each of which contains the logic and subsequently loads a view defined in xml. When starting a new screen, usually a new activity/fragment is created.

 

It seems to me that in Blackberry 10, the only way to do something similar is to add another qml template to the NavigationPane. Which is like creating a view from another view, without going throught the business logic. And an application tend to have one root, eg: 

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

This is correct? Or can the program be organized differenty?

Developer
BGmot
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook

Re: Anyway to have an onLoad function in qml when adding to NavigationPane?

I am still confused by what you are asking.

You can create new pages(views if you wish) using a method you posted from separate qml file or have them reside in one large QML file as Attached Object. There is number of ways to switch between different views. Yuo can have multiple roots as well but obviously at any given moment you can see only one page/view.

New Contributor
sinkpoint
Posts: 3
Registered: ‎02-10-2013
My Device: none

Re: Anyway to have an onLoad function in qml when adding to NavigationPane?

Hi, I was just wondering what is the ideal way to organize the program in BBM 10. 

Coming from Android I was used to the Android mindset. So I was just wondering if there's an equivalent way of doing things. 

 

Having read the docs further I see that things are organized quite differently. 

 

Thanks for your help. :smileyhappy:

Developer
aklowther
Posts: 47
Registered: ‎01-15-2013
My Device: Z10

Re: Anyway to have an onLoad function in qml when adding to NavigationPane?

If you are writing your UI in QML, you can pass C++ objects to your QML files using Context Properties