03-01-2013 05:04 AM
Hello Guys,
Can we push a cpp file from QML ? Some thing like
ActionItem {
title: "Show Next Page"
onTriggered: {
var page = pageDefinition.createObject();
navgationPane.push(page);
}
attachedObjects: ComponentDefinition {
id: pageDefinition
source:"sample.qml"
}
}
Above code pushes QML file onto the UI.The same way's, i would like to push a cpp file on to UI.
Possible solutions please?
Thanks,
Sha.
Solved! Go to Solution.
03-01-2013 05:17 AM
03-01-2013 05:17 AM
Hi,
Please take a look at "Using C++ classes in QML" and below sections in this document:
http://developer.blackberry.com/cascades/documenta
If you export C++ class following the guide above, then it's possible to create it using ComponentDefinition as well:
attachedObjects: [
...,
ComponentDefinition {
id: myComponentDefinition
content: MyComponentExportedFromCPP {}
}
]
There are other options as well such as creating the instance in C++ and exporting it to QML, or creating the instance in C++ and adding it to object hierarchy from C++ code etc.
03-01-2013 06:16 AM
Thanks for the quick response.
My cpp constructor is having parmaeters such as Application* .
Will i have to do some changes to the code snippet mentioned above?
03-01-2013 06:35 AM - edited 03-01-2013 06:38 AM
There's no way to give parameters to constructors in QML as far as I know. One option is to assign default values to them, such as:
Constructor(Application *app = NULL, ...) so the item can be constructed in QML, and exporting these parameters as properties too. Then I believe the properties can be set this way:
content: MyComponentExportedFromCPP {
property: value
}
You'll have to export objects which are used during initialization to QML as well. Injecting the component from C++ code might be easier. This way you can call a single function in C++ code and create & initialize the object there.
Btw, passing Application object between object instances isn't really needed. It's a singleton and can be accessed from anywhere:
Application *app = Application::instance();
03-01-2013 06:49 AM
Thanks for the clarification.
And also, how can I push a cpp file using Navigation pane. I tried to access the object of the Navgation pane in cpp and if i call a push fu nction, it's only accepting Page as parameter.But, how to push a Qobject class to NAvigation pane?
Am i clear?
Regards,
Sha.
03-01-2013 07:20 AM - edited 03-01-2013 07:27 AM
Zmey is right you pass parameters though properties exposed in the C++ class.
For the navigation pane, the only valid object is a 'Page' so you will need to derive from that or place Page in your Componant definition.
var page = pageDefinition.createObject();
navigationPane.push(page);
...
attachedObjects: [
Page {
id: pageDefinition
MyCPPComponant {
myproperty: "hello"
}
}
]
03-01-2013 08:23 AM
I will exactly write down my problem.
I'm having 2 Screens with 2 QML's in place.
My first Screen is directly invoked my Main class. So no problem here.
I'm binding this Screen with a QML which has Navigation pane in it.
Comming to second screen, the QML page is having a custom component which will have a button. I would like to have a click and some other events on this button. As pe my knowledge, i can't initiate an onclick of custom componenet's button from QML.
For this purpose, I tried to write a cpp class, bind it with QML and tried to access the Custom componet's button and performing click functionality.
If I'm trying this way, i'm failing in using navigation pane.
else if i push 2nd QML from 1st QML, i'm failing to bind the cpp file to QML where click functionaity is defined.
Suggest me the possible solution.
Thanks for your patience.
Sha.
03-01-2013 09:38 AM - edited 03-01-2013 09:45 AM
Nothing you've written is impossible, including initiating an onClick from QML (use onTouch or the Tap gesture) , at this stage you're going to have to post your code for us to look at.
However with no 'likes' and just a stream of new questions so far for the help received I'm starting to lose interest in your problem.
Your original question has been answered I believe so please mark a solution either one from Zmey or from me and then post the new problem with code in a new thread. I would suggest Zmeys first post as the one to mark as the most complete solution to your original question.