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
Developer
Posts: 374
Registered: ‎02-12-2011
My Device: Bold 9780
Accepted Solution

Pass QML Data to C++ Function Parameters

I want to pass various QML data from my page to a C++ function for it to be used/saved in C++.

 

How should I do this? I have an action item like so

 

actions: [
        ActionItem {
            title: "Save"
            onTriggered: app.saveData(nameField.text)
            ActionBar.placement: ActionBarPlacement.OnBar
        }
    ]

 saveData is being invoked but the QML throws an error saying unknown method parameter string, I am not sure how I should pass the data so I can get it and save as a string in the C++ code. Also how would one pass other types of data such as a datefield, boolean values from checkboxes, etc to a C++ function,

Developer
Posts: 107
Registered: ‎05-11-2011
My Device: BlackBerry Z10 LE

Re: Pass QML Data to C++ Function Parameters

Hi,


Qml

import bb.cascades 1.0

Page {
    content: Container {
        TextField {
            id: txtField

        }
    }
    actions: [
        ActionItem {
            title: "Save"
            onTriggered: app.loadData(txtField.text)
            ActionBar.placement: ActionBarPlacement.OnBar
        }
    ]
}

.hpp

	Q_INVOKABLE
	void loadData(QString);

.cpp

void ApplicationUI::loadData(QString str) {
	qDebug() << str;
}

 :Devil2:

Developer
Posts: 16,575
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Pass QML Data to C++ Function Parameters

Just to add to Rajs post:
In most cases the type is fine (in this case, QString). Sometimes there can be issues with more complex classes, in these occasions just use QObject* in the method definition.
(does not apply in your sample case)
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter