Search code examples
c++qmlblackberry-10blackberry-cascades

How to load a second .qml file in BB 10 cascade


What I want is, for a button (in one.qml) click load a separate .cpp file (second.cpp) and in that .cpp file, call the second.qml file. How can I do that using BB10 C++ coding. But not using QML coding. I have done it with QML coding BUT that is NOT what I want.

If I compare with Android development, from one Activity we call a second Activity which has a separate .xml file. That's the exact thing I want to test here in BB10.


Solution

  • To load a new QML document from C++, you have to use something like that:

    bb::cascades::QmlDocument *qml = bb::cascades::QmlDocument::create("asset:///yourSecondFile.qml");
    // You can define properties for your page
    qml->setContextProperty("_propertyName", yourObject);
    bb::cascades::Page *secondPage = qml->createRootObject();

    See the QmlDocument documentation for a complete explanation of what you can do with your document.

    So, now, you have your Page (or any other component). You'll need to push it, for example, on a NavigationPane. You can do this that way:

    // Create a back button
    bb::cascades::ActionItem* backAction = bb::cascades::ActionItem::create()
        .title(tr("Previous page"))
        .imageSource(QUrl("asset:///back.png"))
        .onTriggered(navigationPane, SLOT(pop())
    );
    
    // Push the page
    navigationPane->push(page
        .paneProperties(bb::cascades::NavigationPaneProperties::create()
            .backButton(backAction)
        )
    );