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: 455
Registered: ‎03-04-2011
My Device: Paper cup

Application class missing from QML

I've created a new "BlackBerry Cascades C++" project using the NDK, and the Application class is not recognized in QML.

 

Have been trying things like adding   qml->setContextProperty("Application", app);  or   qml->setContextProperty("app", this);  but no difference.

 

 

It's just a basic blank app created by the IDE, and one UI component that's supposed to quit:

 

 

MyApp.hpp:

#ifndef MyApp_HPP_
#define MyApp_HPP_

#include <QObject>

namespace bb { namespace cascades { class Application; }}

class MyApp : public QObject
{
    Q_OBJECT
public:
    MyApp(bb::cascades::Application *app);
    virtual ~MyApp() {}
};


#endif 

 

MyApp.cpp :

#include "MyApp.hpp"

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

using namespace bb::cascades;

MyApp::MyApp(bb::cascades::Application *app)
: QObject(app)
{
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

    AbstractPane *root = qml->createRootObject<AbstractPane>();

    app->setScene(root);
}

 

main.cpp :

 

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

#include <QLocale>
#include <QTranslator>
#include "MyApp.hpp"

using namespace bb::cascades;

int main(int argc, char **argv)
{
    Application app(argc, argv);

    QTranslator translator;
    QString locale_string = QLocale().name();
    QString filename = QString( "MyApp_%1" ).arg( locale_string );
    if (translator.load(filename, "app/native/qm")) {
        app.installTranslator( &translator );
    }

    new MyApp(&app);

    return Application::exec();
}

 

main.qml :

 

import bb.cascades 1.0

Page {
    Container {
            Slider {
                id: slideToQuit
                toValue: 100.0
                fromValue: 0.0
                value: 100.0
                onValueChanged: {
                    if (value < 7) {
                        Application.quit();                
                    }
                }
            }
    }
}

 

 

Any help would be appreciated.  I've spent hours on this...

 

thanks

 

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Application class missing from QML

[ Edited ]

Hi,

 

You're exporting an instance of your own class (MyApp), not the Cascades application class.

And there's no function named quit in it.

 

 

Try:

qml->setContextProperty("app", this);  

 

In QML call:

app.quit();

 In MyApp.hpp in public section declare

 

Q_INVOKABLE void quit();

(optionally a slot can be used instead). 

 

 

Implement the function in MyApp.cpp:

void MyApp::quit() {
  Application::instance()->requestExit();
}

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Application class missing from QML

[ Edited ]

Edit: What zmey said Smiley Happy

also note this:
http://supportforums.blackberry.com/t5/Cascades-Development/How-to-exit-finish-and-application/td-p/...

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter