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
Contributor
doug1010
Posts: 14
Registered: ‎04-25-2011
My Device: Playbook and Bold 9700
My Carrier: Rogers

Re: How can i use c++ and qml at the same time?

Thanks very much guys!  I will try out your suggestions!

Sorry if my post was consufing as to what I was trying to do.

I just wanted to make a simple test to see how to get c++ to set a property in qml from where I want.  I would change what exactly it does later.

 

pseudo code would be something like:

 

Main.qml

 

myLabel { }
myButton {
onClicked: {
MyApp.chgLabelText("NewText");
}
}

 

MyApp.cpp

 

MyApp::MyApp(bb::cascades::Application *app) : QObject(app)
{ define root etc }

MyApp::chgLabelText(myNewString)
{
//change the text in myLabel defined in Main.qml when I click myButton
//something like:
root->setProperty("myLabel_Text", myNewString);
}
Please use plain text.
Developer
thubalek
Posts: 322
Registered: ‎10-01-2012
My Device: Dev Alpha C, Playbook 32 GB
My Carrier: BleskMobil

Re: How can i use c++ and qml at the same time?

You don't need so tight coupling. Look at my example, it is very easy and does exactly what you want. 

 

It takes two numbers from QML, sends them to C++, sums them and then asks from QML for results. 

 

 

 

--------------------------------------------------------------------------------------------
-- My Blackberry 10 app at http://blackberry.hubalek.net
-- My Android apps at http://android.hubalek.net
Please use plain text.
New Contributor
ajalkane
Posts: 4
Registered: ‎01-17-2013
My Device: Dev Alpha B
My Carrier: RIM

Re: How can i use c++ and qml at the same time?

 MyApp.cpp

 

MyApp::MyApp(bb::cascades::Application *app) : QObject(app)
{ define root etc }

MyApp::chgLabelText(myNewString)
{
//change the text in myLabel defined in Main.qml when I click myButton
//something like:
root->setProperty("myLabel_Text", myNewString);
}

This is wrong approach. What you want is something like this (I only show the important bits to make it easier to understand the changes needed):

 

Page {
  Label {
    text: app.customLabel
  } 
  myButton {
    onClicked: { 
        app.customLabel = "New text" 
    }
  }
}

class MyApp : public QObject
{
    Q_OBJECT

    Q_PROPERTY(QString customLabel READ getCustomLabel WRITE setCustomLabel NOTIFY customLabelChanged)

    QString m_customLabel;
signals:
    customLabelChanged();

public:
    MyApp(bb::cascades::Application *app);

    QString getCustomLabel() const;
    void setCustomLabel(const QString &label);

    Q_INVOKABLE void chgText(QString myString);
};

QString
MyApp::getCustomLabel() const { return m_customLabel; }

QString
MyApp::setCustomLabel(const QString &label) { 
    if (label != m_customLabel) {
        m_customLabel = label;
        emit customLabelChanged;
    }
}

MyApp::MyApp(bb::cascades::Application *app)
: QObject(app)
{
    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
    AbstractPane *root = qml->createRootObject<AbstractPane>();
    
    qml->setContextProperty("app", this);

    app->setScene(root);
}

 

 

 

Please use plain text.
Contributor
doug1010
Posts: 14
Registered: ‎04-25-2011
My Device: Playbook and Bold 9700
My Carrier: Rogers

Re: How can i use c++ and qml at the same time?

Gassssp! I got it to work!  Thanks so much thubalek and ajalkane! I really appreciate it! :smileyvery-happy:

Please use plain text.