Mayank
Mayank

Reputation: 1139

Event Handling in BB10

I am developing a BB10 app that contains a login screen asking for Username and Password of the user. After that user presses the submit button.

I have designed the UI using QML

I want to capture the information given by the user (Username and password) and send it to a web service for verification.

I want to capture the information in a C++ class.

Can anyone suggest how can I accomplish this ?

Upvotes: 1

Views: 436

Answers (2)

iamanyone
iamanyone

Reputation: 429

To answer the first part of your question:
"I want to capture the information given by the user"

In your qml

Button {
    text: "Login"
    onClicked: {
        myQMLObj.login(userTextField.text,passTextField.text);
    }
}

In your namehere.hpp

public:
      // "Q_INVOKABLE" allows this function to be called from qml
      Q_INVOKABLE void login(QString user,QString pass);

In your namehere.cpp

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

    // expose C++ object in QML as an variable (so you can fire your login function
    qml->setContextProperty("myQMLObj", this);

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

void namehere::login(QString user, QString pass) {
    // Handle the user name & pass here
}

Hope that helps you atleast capture the data from the user.
After that you can begin to send it to a web service


Just found this example, which explains alot better then me :)

Upvotes: 3

shernshiou
shernshiou

Reputation: 518

First write a class that include QNetworkAccessManager, QNetworkReply and maybe JsonDataAccess

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <bb/data/JsonDataAccess>
#include <bb/system/SystemToast>

using namespace bb::data;
using namespace bb::system;

class Member : public QObject
{
    Q_OBJECT

public:
    Member();
    virtual ~Member();

    Q_INVOKABLE void login(QString username, QString password);

private:
    QNetworkAccessManager *networkManager;
    QString username;
    QString password;

signals:
    void serverReply();

private slots:
    void replyFinished(QNetworkReply*);
};

Then, load the class into QML

qmlRegisterType<Member>("com.library", 1, 0, "Member");

Import the library package into QML by adding

import com.library 1.0

Then initialize the member class at attachedObjects at QML. You can call login method as it is in Q_INVOKABLE form. Hope this help.

Upvotes: 0

Related Questions