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
Developer
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

How to load QML documents on the fly from C++

I am developing a large application and I would like to be able to push QML documents on the fly from a C++ controller. I am not satisfied with using only NavigationPane. How do I load a QML file from C++ on the fly?

 

Thanks,

Scott

Developer
Posts: 49
Registered: ‎08-21-2012
My Device: Blackberry z10 LE
My Carrier: Vodafone

Re: How to load QML documents on the fly from C++

Find my answer in thi thread:

http://qt-project.org/forums/viewthread/18740/

 

there are two solution explained:

1 - using QDeclarativeComponent/Item I've been written

2 - using setData of QDeclarativeComponent (shown after some asnwers)

3 - Using global Qt heper, like in doc: http://doc.qt.nokia.com/4.7/qml-qt.html#createQmlObject-method

Nokia Qt Ambassador
http://www.softairrealfight.net
Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: How to load QML documents on the fly from C++

Is your question how to read the QML or how to display it?

 

See sample cascadescookbookcpp, particularly:

CascadesCookbookApp::createContentPage()

 

I would try using QmlDocument to load the qml (https://developer.blackberry.com/cascades/reference/bb__cascades__qmldocument.html) then update the UI how you like, e.g. Page::setContent (but beware of ownership), or if pages are dynamic and not reused, create a custom component that you add and remove from a Container.

 

I have not tried these myself yet; let us know if this helps, or not.

 

Stuart

 

 

 

Developer
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: How to load QML documents on the fly from C++

Thanks for the responses.

 

I should give a little more information. I want to avoid, as much as possible, defining the layout from a C++ context and do most of the layout in QML because QML seems like a very easy way to create layouts. It seems every tutorial on the Cascades site is showing how to either make an app entirely in QML or entirely in C++, but there is little mingling between the two.

 

Following the format in the QML tutorials seems to indicate that if I want to use QML for the layout, all the QML must be in one file unless you use NavigationPane. I can't define a 40 screen application in one QML file and I can't use NavigationPane for the entire application. As a consequence I am looking for ways to load and display QML files from a controller that I can set up from C++. For example, if a button is clicked, I want to signal to the controller to display a new QML page among other things. 

 

The CascadesCookbookApp example does not explain how to set up an application framework like this because it shows how to layout an application entirely in C++ or entirely in QML. The QmlDocument API indicates how to load and display a QML document from file, however using Application::setScene a second time freezes and closes my application.

 

AlterX, from your link it is not clear to me how I would load and display a QML document. Could you please explain more?

 

Here is the code I am working with:

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>

#include "app.hpp"

using namespace bb::cascades;

App::App()
{
    QmlDocument *qml = QmlDocument::create("main.qml");
    QDeclarativeContext *qdc = qml->documentContext();
    qdc->setContextProperty("controller", this);
    AbstractPane *root = qml->createRootNode<AbstractPane>();
    Application::setScene(root);
}

void App::displayAccountsSelectionScreen() {
    // I want to display next.qml from here
}

The onClicked function of a button calls displayAccountsSelectionScreen(). From this function I would like to display a screen defined in a separate QML file.

 

Thanks,

Scott

Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: How to load QML documents on the fly from C++

I'm not sure what the stumbling block is.   You can create pages in C++ or in QML.  You can handle signals in C++ or in QML.   You can set content or push a page in C++ or QML.   It doesn't have to be all one or all another.

 

Perhaps the samples stampcollector and weatherguesser are clearer?

 

Stuart

Developer
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: How to load QML documents on the fly from C++

Stamp Collector uses NavigationPane, which I have told you a few times will not work for my application. NavigationPane produces a bottom navigation bar that devotes 15% or so of the screen space to a single back button and custom buttons cannot be added to this bar - this causes wasted screen real estate. Not only is it illogical to have a back button on certain screens, on screens where it makes sense to have a back button NavigationPane presents a design constraint because of the wasted screen real estate.

 

The Weather Guesser tutorial uses TabbedPane. Similar to NavigationPane, TabbedPane will allow me to load QML files but I have many screens that are not appropriate to use tab bars.

 

The most direct way of answering my question would be to complete the code I presented above.

 

Scott

Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: How to load QML documents on the fly from C++

I have some thoughts and suggestion I hope will help, but there are details I haven't had a chance to play with so there may be better ways.   Hopefully this will trigger some thoughts for how you want to proceed.

 

So you really have 3 questions:

1. What should the main page content be

2. How should we manage all the qml

3. How do I load the qml

 

A few points first:

- definitely do the layout as much as possible in QML if you can

- yes you can use many qml files AND manage them in C++

- cascadescookbookqml defines lots of pages in separate qml

- cascadescookbookcpp shows among other things how to load qml and make the currrent page.

- " Application::setScene a second time freezes and closes my application." -- sounds like it's deleting an object you are trying to re-use.  This might require further investigation.  If you can set up a trivial example please start a separate thread.

 

Things to check out:

- sheets

- tabs on the side

- action items on the overflow

- define your own components

 

It sounds to me like you are looking for a page with action items on the side.  Due to object ownership, perhaps you want a page with a container that in turn has two overlaid containers, one visible one not.  You select a new component and replace it in the hidden container, then swap which container is visible.  This might be more complicated than you need, and I haven't tried it.

 

For the number of objects you have, I would certainly define components in separate QML files.

As for what your main page should be, it depends on how you expect the user to navigate.  If it doesn't feel like tabs and doesn't feel like the push/pop context of a navigation page, then you probably want either just a Page or Sheets in combination with other panes.

 

I suggest you read:

http://supportforums.blackberry.com/t5/Cascades-Development/attachedObjects-property-in-qml/m-p/1887...

https://developer.blackberry.com/cascades/documentation/ui/signals_slots/signals_slots_set_up_projec...

 

As for the 3rd question, just load the qml in C++ and set the content of the container.  Manage the lifetime of this object you create (and verify that by adding and removing it from the container you maintain ownership of the component -- I have not played with how much Page tries to own).   If the mechanics of this are unclear after the above examples and the previous posts in this thread, please ask again.

 

Stuart

 

 

Developer
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: How to load QML documents on the fly from C++

Thanks, Stuart.

 

Cascades Cookbook uses NavigationPane so it doesn't help in this case. Also, the C++ version does not use any QML. The method used to load and display QML files in NavigationPane only applies to NavigationPane. I've actually looked through just about every sample app and they all use either TabbedPane or NavigationPane to manage multiple QML files.

 

There is a lot here for me to chew on. Thanks.

 

Scott

Retired
Posts: 499
Registered: ‎05-07-2012
My Device: developer
My Carrier: developer

Re: How to load QML documents on the fly from C++

[ Edited ]

Well, depending on what your component/pane ACTUALLY is there should be a method to change it.

e.g.

NavigationPane:Smiley Tongueush, pop

TabbedPane::add, insert, remove, removeAll

Page::setContent

Container::insert, remove

cascades::Application::setScene

 

The code to create the component from the QML will be the same.

The code to find an object from the QML will be the same.

 

Stuart

 

Retired
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: How to load QML documents on the fly from C++


superdirt wrote:

Stamp Collector uses NavigationPane, which I have told you a few times will not work for my application. NavigationPane produces a bottom navigation bar that devotes 15% or so of the screen space to a single back button and custom buttons cannot be added to this bar - this causes wasted screen real estate. Not only is it illogical to have a back button on certain screens, on screens where it makes sense to have a back button NavigationPane presents a design constraint because of the wasted screen real estate.

 


Hi Scott, 

 

I just wanted to jump in hear and quickly let you know that you have the ability to add menu items to the Action Bar with custom images. You also have the ability to hide the back button and even the entire ActionBar, you really have loads of control over the ActionBar. Then you have the ability to Push/Pop screens using the methods provided by Stuart. You can use the push/pop paradigm of the NavigationPane without ever displaying an ActionBar if desired, or hide the ActionBar to give more real estate to the content.

Garett
@garettBeuk
--
Goodbye everybody!