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
smartek
Posts: 281
Registered: ‎05-31-2012
My Device: Playbook
My Carrier: o2
Accepted Solution

howto simply reference a QML element and then work with it?

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?

Plase "+Like" my post if it was helpful.
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: howto simply reference a QML element and then work with it?

It would generally be considered a bad idea to have the C++ code access individual QML elements like that, so rather than show an answer (since it is possible), I'll ask you to describe why you want to do it so we can be sure of providing a better approach, if there is one.

The only use case that's probably not a "bad idea" here would be if you were doing this for testing purposes, where you had a C++ test framework that was trying to manipulate and/or inspect specific elements.

If this is for the real app logic, you probably need to be learning more about Properties and Signals/Slots, but I'll wait for your answer before diving why that would be better.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
smartek
Posts: 281
Registered: ‎05-31-2012
My Device: Playbook
My Carrier: o2

Re: howto simply reference a QML element and then work with it?

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

Plase "+Like" my post if it was helpful.
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: howto simply reference a QML element and then work with it?

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 .


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
smartek
Posts: 281
Registered: ‎05-31-2012
My Device: Playbook
My Carrier: o2

Re: howto simply reference a QML element and then work with it?

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?

Plase "+Like" my post if it was helpful.
Please use plain text.
Contributor
djmbb
Posts: 30
Registered: ‎06-14-2012
My Device: Simulator
My Carrier: all

Re: howto simply reference a QML element and then work with it?

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;
}
//-------------------------------------------------

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: howto simply reference a QML element and then work with it?

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.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
smartek
Posts: 281
Registered: ‎05-31-2012
My Device: Playbook
My Carrier: o2

Re: howto simply reference a QML element and then work with it?

okay, my question is nearly fully answered :smileyhappy:

 

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?

Plase "+Like" my post if it was helpful.
Please use plain text.
Contributor
djmbb
Posts: 30
Registered: ‎06-14-2012
My Device: Simulator
My Carrier: all

Re: howto simply reference a QML element and then work with it?

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

Please use plain text.
Developer
level32
Posts: 52
Registered: ‎03-29-2012
My Device: Playbook, Dev Alpha
My Carrier: Telus

Re: howto simply reference a QML element and then work with it?

Please use plain text.