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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Contributor
Posts: 13
Registered: ‎03-06-2013
My Device: Blackberry 10, blackberry java
My Carrier: Movistar
Accepted Solution

getElementById in qml

does it exist some function as getElementById from javascript in qml?


A function for to access to one id with a specified text string  and to make concatenated ids.

 

thanks

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: getElementById in qml

I didn't find any. Smiley Sad

Had to create a helper function in C++ to use findChild<> and objectName.

This can also be done in pure QML by iterating item's children, but exporting findChild is simpler.

 

Utils.h

#ifndef UTILS_H
#define UTILS_H

#include <QObject>

class Utils : public QObject
{
        Q_OBJECT
public:
        explicit Utils(QObject *parent = 0);
        static Utils *sharedInstance();

        Q_INVOKABLE QObject *findChild(QObject *parent, QString name);

signals:

public slots:

};

#endif // UTILS_H

Utils.cpp:

 

#include "Utils.h"

Utils::Utils(QObject *parent)
        : QObject(parent)
{
}

Utils *Utils::sharedInstance()
{
        static Utils instance; 
        return &instance;
}
        
QObject *Utils::findChild(QObject *parent, QString name)
{
        return parent->findChild<QObject *>(name);
}

 ...

rootObject->setContextProperty("_utils", Utils::sharedInstance());

 

 

 

 

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 323
Registered: ‎05-07-2009
My Device: BlackBerry Z10, PlayBook, 9900
My Carrier: Telus

Re: getElementById in qml

I might be misunderstanding the question but if you are trying to access a QML element by its ID... You can access it directly.

Eg if you have a button with the ID of "someButton" you can access it like:

someButton.text = "New Label!"

PlayBook Apps: DataMine, Dots + Boxes, 5 Marbles
BlackBerry SmartPhone Apps: 5 Marbles Soon: **** (D.Z.A), ****Quest, **** Challenge, VaultCracker, DevBrowser, Radial****
Contributor
Posts: 13
Registered: ‎03-06-2013
My Device: Blackberry 10, blackberry java
My Carrier: Movistar

Re: getElementById in qml

my intention is access to elements by string formed id for to make for example:

 

for(var nCont = 0; nCont < 100; nCont++)
     getElementById("button" + nCont).title = "anything"

 

knowing that exist one hundred buttons whose ids are: button0, button1, to button99.

Highlighted
Contributor
Posts: 13
Registered: ‎03-06-2013
My Device: Blackberry 10, blackberry java
My Carrier: Movistar

Re: getElementById in qml

how to use that? for example:

 

for(var nCont = 0; nCont < 100; nCont++)

{

    var currentButton = _utils.findChild(idParent, "button" + nCont);

    ...

}

 

can the parent to be the root of document, or the parent has to be the element that directly contains the buttons?

New Contributor
Posts: 6
Registered: ‎07-22-2012
My Device: Playbook
My Carrier: -

Re: getElementById in qml

You can use JavaScript eval():

 

for(var nCont = 0; nCont < 100; nCont++)
     eval("button" + nCont + ".text = 'anything'");

 

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: getElementById in qml

[ Edited ]

Yes, it looks correct.

 

findChild searches elements recursively, so parent can be any item, but for lesser overhead it's better to specify the element that directly contains the buttons.

 

If you don't need recursive search, a pure-QML approach would be (it's also more efficient because children are iterated only once):

if (item.children)
{
    var searchString = "button"

    for (var idx in item.children) {
        var currentButton = item.children[idx]

        if (currentButton.objectName.match("^"+searchString) !== searchString)
          continue;

console.log("found button: " + currentButton.objectName)
// This button has objectName starting with "button" if (currentButton.somePropertyToCheck) { // do something } } }

The code is untested. Not sure if my beginsWith implementation is correct, this can be done multiple ways, for example by taking a substring of first 6 symbols and comparing it to 'button'.

 

Btw, objectName should contain quotes. I often forget to add them. Smiley Happy
Item { objectName: "button1" }

p.s. @klik: nice shortcut with eval!


Andrey Fidrya, @zmeyc on twitter
Contributor
Posts: 13
Registered: ‎03-06-2013
My Device: Blackberry 10, blackberry java
My Carrier: Movistar

Re: getElementById in qml

yes, it works

 

thanks