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
New Developer
Posts: 133
Registered: ‎10-01-2012
My Device: Playbook
My Carrier: CMCC
Accepted Solution

Stuck at calling c++ function from QML

Hi,

I am a newbie in QML. While playing with examples, I tried to call c++ function from qml. 

 

main.qml is-

import bb.cascades 1.0

Page {
    Container {
        layout: StackLayout {
        }
        Label {
            text:  _foo.sayHello();   
        }
    }
}

 c++ is

//say Hello
QString foo::sayHello()
{
	QString hello = "Hello!";
	return hello;
}

 But my program is not saying hello. What is wrong?

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

Re: Stuck at calling c++ function from QML

[ Edited ]

Could you show the function declaration and how the class is exported to QML?
The function should be declared as Q_INVOKABLE.

 

Checking the device logs for runtime errors might also give a hint.

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 293
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: Stuck at calling c++ function from QML

1. Is your function Q_INVOKABLE
2. Is the semi-colon necessary in a QML property-binding statement?

You also might consider using a Q_PROPERTY instead of a direct function call. This will bind the QML to the property in your C++ object, such that every time the value in C++ changes, the label text field will automatically update

New Developer
Posts: 133
Registered: ‎10-01-2012
My Device: Playbook
My Carrier: CMCC

Re: Stuck at calling c++ function from QML

Thanks a bunch! it solves the problem Smiley Happy
New Developer
Posts: 133
Registered: ‎10-01-2012
My Device: Playbook
My Carrier: CMCC

Re: Stuck at calling c++ function from QML

Thanks for your reply. I am interested in Q_PROPERTY. It will be very helpful to me if you please mention a simple example using it.
Developer
Posts: 293
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: Stuck at calling c++ function from QML