Log in / Register
Home arrow Computer Science arrow Learn BlackBerry 10 App Development
< Prev   CONTENTS   Next >

Cascades Application Bootstrap Process

The entry point for all Cascades applications is the main function shown in Listing 1-6.

Listing 1-6. main.cpp

#include <bb/cascades/Application>

#include <QLocale>

#include <QTranslator>

#include "applicationui.hpp"

#include <Qt/qdeclarativedebug.h> using namespace bb::cascades;

Q_DECL_EXPORT int main(int argc, char **argv)


Application app(argc, argv);

// Create the Application UI object, this is where the main.qml file

// is loaded and the application scene is set. new ApplicationUI(&app);

// Enter the application main event loop. return Application::exec();


The first step in main is to create an instance of a bb::cascades::Application class, which provides the application's run loop, and all the boilerplate functionality required by a Cascades application. At this point, you will have a “bare bones” Cascades app but the run loop has not kicked in yet.

To further customize the application, the following properties of the bb::Cascades::Application

instance have to be specified:

n Scene property: Specifies the instance of bb::cascades::AbstractPane to use as the scene for the application's main window. A scene is basically a layout of controls which will be displayed in the application's main window.

n Cover property: Specifies the instance of bb::cascades::AbstractCover to be used when the application is in cover mode.

n Menu property: An instance of a bb::cascades::Menu accessible by the user with a swipe from the top of the screen.

In practice, you will not update the bb::cascades::Application's properties directly in the main function but instead rely on an application delegate object, which will take care of loading or creating the main scene and wiring all the events using signals and slots. You've already seen an implementation of an application delegate in Listing 1-2 and Listing 1-3 given by the ApplicationUI class. In Listing 1-3, we customized the application delegate in order to build the scene graph using C++. Listing 1-7 shows the default version generated by the Momentics IDE's New BlackBerry Project wizard (more on installing your development environment later in the chapter).

Listing 1-7. applicationui.cpp

ApplicationUI::ApplicationUI(bb::cascades::Application *app) : QObject(app)


// prepare the localization. Code omitted

// Create scene document from main.qml asset, the parent is set

// to ensure the document gets destroyed properly at shut down. 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 the application scene app->setScene(root);


I've removed the code related to localization in order to concentrate on the scene graph creation logic. Here an instance of a bb::cascades::QmlDocument is created by reading the main.qml QML file containing the declarative UI description. This is the same QML you will design using the Cascades Builder tool.

Finally, once the application delegate has been initialized, the application's main event loop kicks in through a call to bb::cascades::Application::exec().

Parent-Child Ownership

If you take a close look at Listing 1-3, you will notice that I haven't released the objects allocated with the new operator at any point in the code. This might seem as a memory leak but it's not. Cascades widgets are organized in a parent-child relationship that also handles object ownership and memory management. In the case shown in Listing 1-3, the root parent of the entire object hierarchy is the bb::cascades::Application app object. The memory associated with the child controls will be released when this object is deleted by the runtime. I will cover memory management in detail in Chapter 3, but for the moment you can safely assume that there are no memory leaks in Listing 1-3.

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science