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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Contributor
Posts: 17
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);
}
Developer
Posts: 325
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
New Contributor
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);
}

 

 

 

Highlighted
Contributor
Posts: 17
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! Smiley Very Happy