UI does not respond outside ApplicationUI

69 Views Asked by At

I'm new developing BlackBerry applications and it's really hard for me understand how it works because the way to develop for BB differs a lot of Android or iOS developing.

Here is my issue: I've created a registerPage.qml and a registerPageController (with it's .hpp and .cpp). I would like the app starts with the registerPage.qml and call form the qml some methods i wrote in the .cpp file.

If I create the qml document outside the ApplicationUI(), the page is displayed nice, but the buttons does not respond.

I think with the code is easily to understand:

applicationui.cpp

ApplicationUI::ApplicationUI() : QObject()
{
    m_pTranslator = new QTranslator(this);
    m_pLocaleHandler = new LocaleHandler(this);

    bool res = QObject::connect(m_pLocaleHandler, SIGNAL(systemLanguageChanged()), this, SLOT(onSystemLanguageChanged()));
    Q_ASSERT(res);
    Q_UNUSED(res);

    onSystemLanguageChanged();

    // --> OPTION 1 (what I want) <--
    /* If I init the register page here, the page is
       displayed ok, but buttons does not respond.
       The RegisterPage initialization code is below. */
    // RegisterPage registerPage;

    // --> OPTION 2 <--
    /* If I create the registerPage here, buttons respond
       but the _register param is not setted properly*/
       QmlDocument *qml = QmlDocument::create("asset:///registerPage.qml").parent(this);
       qml->setContextProperty("_register", this);

       AbstractPane *root = qml->createRootObject<AbstractPane>();
       Application::instance()->setScene(root);
}

registerPage.hpp

class RegisterPage: public QObject
{
    Q_OBJECT
public:
    RegisterPage();
    virtual ~RegisterPage() {}

    Q_INVOKABLE void initRegistration();
};

registerPage.cpp

RegisterPage::RegisterPage()
{
    /* With this initialization, buttons does not respond!!*/
    QmlDocument *qml = QmlDocument::create("asset:///registerPage.qml").parent(this);
    qml->setContextProperty("_register", this);

    // Create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();


    // Set created root object as the application scene
    Application::instance()->setScene(root);
}

void RegisterPage::initRegistration()
{
    qDebug() << "start registration!";
}

registerPage.qml

Page {
    Container {
        Button {
            text: "Accept"
            horizontalAlignment: HorizontalAlignment.Fill
            topMargin: 100.0
            appearance: ControlAppearance.Primary
            onClicked: {
                console.log("click!!")
                _register.initRegistration()
            }
        }
    }
}

How can I load an qml file, associate it an .cpp and call functions from qml? Why the buttons does not respond?

Thanks a lot, and sorry if this is the basis of BlackBerry development, but this is making me crazy.

----------------

EDITED

Finally thanks to @Filip Hazubski to help me to find the final solution.

In applicationui.cpp

QmlDocument *qml = QmlDocument::create("asset:///registerPage.qml").parent(this);
AbstractPane *root = qml->createRootObject<AbstractPane>();

RegisterPage* registerPage = new RegisterPage(this, root);
qml->setContextProperty("_register", registerPage);

Application::instance()->setScene(root);

and in registerPage.cpp

RegisterPage::RegisterPage(QObject *parent, AbstractPane *root) : QObject(parent)
{
    phoneTextField = root->findChild<TextField*>("phoneTextField");
}

Passing the AbstractPane as param we can find the qml elements into the registerPage.cpp.

Thanks!

1

There are 1 best solutions below

5
Filip Hazubski On BEST ANSWER

I am not sure but maybe my answer will help you.

In applicationui.cpp instead of:

QmlDocument *qml = QmlDocument::create("asset:///registerPage.qml").parent(this);
qml->setContextProperty("_register", this);

AbstractPane *root = qml->createRootObject<AbstractPane>();
Application::instance()->setScene(root);

try:

QmlDocument *qml = QmlDocument::create("asset:///registerPage.qml").parent(this);
AbstractPane *root = qml->createRootObject<AbstractPane>();

RegisterPage* registerPage = new RegisterPage(this);
qml->setContextProperty("_register", registerPage);

Application::instance()->setScene(root);

and in registerPage.hpp

RegisterPage();

change to

RegisterPage(QObject *parent = 0);

and in registerPage.cpp change

RegisterPage::RegisterPage()
{
    /* With this initialization, buttons does not respond!!*/
    QmlDocument *qml = QmlDocument::create("asset:///registerPage.qml").parent(this);
    qml->setContextProperty("_register", this);

    // Create root object for the UI
    AbstractPane *root = qml->createRootObject<AbstractPane>();


    // Set created root object as the application scene
    Application::instance()->setScene(root);
}

to

RegisterPage::RegisterPage(QObject *parent) : QObject(parent)
{
}