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 Contributor
lau_coop
Posts: 8
Registered: ‎06-28-2013
My Device: Blackberry Z10 developer
Accepted Solution

BB10 Cascades : call a C++ function from QML

Hi all,

 

I am having trouble calling a function from a C++ class in QML after transfering an object from one qml to another.

Each QML file has one C++ file where I call the functions.

Here is my code

 

 

the first page : main.qml

ListView {
...
onTriggered: { 

                    // Create the content page and push it on top to drill down to it.
                    var newPage = pageDefinition.createObject();
                    // Set the content properties to reflect the selected image.
                    var objectPage2 = objectPage1.getobjectPage2();
                    
                    newPage.object2 = objectPage2;
                    
                    // Push the content page to the navigation stack
                     navigationPane.push(newPage);
                }
        }

 

the function getobjectPage2() is in the object1.cpp file

object1.cpp

Object2* Object1::getobjectPage2(){
	Object2* obj = new Object2();
	return obj;
}

 

main2.qml

Page {
    property variant object2

    onCreationCompleted: {
       
        object2.callfunction();
    }
Label {
          text: object2.getTitle
       }
}

 object2.hpp

 

Q_PROPERTY(QString getTitle READ getTitle WRITE setTitle NOTIFY titleChanged)

Q_INVOKABLE void callfunction();

 

So when I execute the program, object2.getTitle works, I can get the value but I can't call the callfunction() function. I tried to write a qDebug() inside the function but it doesn't even go in there.

And the function has the Q_INVOKABLE label so I don't understand why it works for elements with Q_PROPERTY but not with Q_INVOKABLE.

 

Can anyone help me understand what's wrong ?

 

Thanks

 

Please use plain text.
Developer
DrShavargo
Posts: 133
Registered: ‎05-10-2013
My Device: Blackberry Z10

Re: BB10 Cascades : call a C++ function from QML

In the C++ file, at the beginning (not in main.cpp though), you call:

 

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

or something similar. To call a c++ function from qml with Q_INVOKABLE, use app.callfunction(), as you defined "app" as the context property, or name, in your c++ file. I don't knwo how you're going to integrate that into your application, but as far as I know, this is the way to call a Q_INVOKABLE method.

----------------------------------------
Remember to mark the thread as solved at the post that solved your problem, and if you like a post, like it!
Please use plain text.
Developer
laairoy
Posts: 24
Registered: ‎10-31-2012
My Device: Dev Alpha B

Re: BB10 Cascades : call a C++ function from QML

qml->setContextProperty("objectPage1", this);
__
Helped you? Like me.
Please use plain text.
New Contributor
lau_coop
Posts: 8
Registered: ‎06-28-2013
My Device: Blackberry Z10 developer

Re: BB10 Cascades : call a C++ function from QML

Thank you for answering.

 

I figured it out by using another method :

 

in object1.cpp I used :

qmlRegisterType<object2>("mylibrary.program", 1, 0, "object2");

 and in main2.qml :

attachedObjects: [
        QTimer {
            id: timer
            interval: 900000
            onTimeout: {
                object2.setCallsWebService();
            }
        },
        object2 {
            id: object2
        }
    ]

 

With this I am able to call the functions I want in main2.cpp. 

 

 

Please use plain text.
Developer
laairoy
Posts: 24
Registered: ‎10-31-2012
My Device: Dev Alpha B

Re: BB10 Cascades : call a C++ function from QML

Very nice!

__
Helped you? Like me.
Please use plain text.