06-17-2012 05:40 AM
hi simple question:
i have a QML layout and now want to access via c++ to the e.g. Button, TextField oder Label
i have the following code:
HelloCascadesApp::HelloCascadesApp()
{
QmlDocument *qml = QmlDocument::create("hellocascades.qml");
qml->setContextProperty("cs", this);
AbstractPane *root = qml->createRootNode<AbstractPane>();
Application::setScene(root);
}is there something like in Android devleopment findViewById?
Solved! Go to Solution.
06-17-2012 09:00 AM
06-17-2012 09:07 AM
I want to make a short example like:
Button, TextField and Label
I want to have a reference to make the following:
after the on click listener of the button was clicked, i want to get the value of the TextField, cast it to a integer and make some calculation and then write it into the text of the Label.
this is a normal approach how i would make it in android/bada/WP7
06-17-2012 09:59 AM
If that were all the app was going to do, then you wouldn't even bother with C++ probably, since that could be done entirely in the QML with Javascript. I'll assume you knew that and this is just an example of what a larger app might do.
Although it's technically possible to do what you're describing, I don't think it's how one is expected to use QML. Rather you would have the button emit a Signal when it was clicked and, having bound that to a Slot in the C++ code you would do the calculations there, then probably update a Property in the C++ class. The Label's text would have been bound to the property at startup, so it would update automatically when you changed the property value.
See examples and background here http://qt-project.org/doc/qt-4.8/qtbinding.html and here http://qt-project.org/doc/qt-4.8/properties.html .
06-18-2012 02:12 AM
okay, thats very nice, thank you.
but another question related to this:
e.g. i have a webservice, i get some data from the webservice throw c++ and now want to display it in a label, hot to make this?
or can i call the webservice also via qml?
06-18-2012 06:13 AM
Its good practice to have a c++ controller for each qml element that needs one. Generally this a single page in your app (but complicated pages can require several c++ (sub) controllers).
The only way the controller has to access (search for) its qml element is by name (strings seach, which is ugly and slow, and requires you to to be careful to allocate unique "objectName"s in the QML). Below is sample code that finds a qml element with a specific name
//------------------------------------------------
QObject* tRpQmlMan::decObj(QString ObjName)
{
QDeclarativeView * DecView = decView();
if(DecView && DecView->rootObject())
return decObj(decView()->rootObject(), ObjName);
else
return 0;
}
//------------------------------------------------
QObject* tRpQmlMan::decObj(QObject *rootElement, QString objectName)
{
if(!rootElement)
return 0;
if(rootElement->objectName() == objectName)
return rootElement;
const QObjectList list = rootElement->children();
for(QObjectList::const_iterator it=list.begin(); it!=list.end(); it++)
{
QObject *object = decObj((*it), objectName);
if(object)
return object;
}
return 0;
}
//------------------------------------------------
06-18-2012 09:13 AM
You can use XMLHttpRequests in Javascript in QML, so you should be able to query the web service directly from QML if that's appropriate. There are certainly examples of this if you search, for example on the Qt site.
06-18-2012 10:30 AM
okay, my question is nearly fully answered ![]()
one last thing:
where and how to write code in QML? i know i can write code e.g. in the onClicked: {} of a button
but where to write code like a function which should be called?
where to write variables which should be used in the QML file?
06-19-2012 05:23 AM
Have a look at
http://doc.qt.nokia.com/4.7/qtbinding.html
"Exchanging data between QML and C++"
Im not sure if the same info is available in the blackberry doc
06-19-2012 12:36 PM
Here's a sample of javascript in qml:
https://developer.blackberry.com/cascades/document