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
Contributor
FedericoTech
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

Please use plain text.
Developer
Zmey
Posts: 1,510
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: getElementById in qml

I didn't find any. :smileysad:

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
Please use plain text.
Developer
steve_web
Posts: 319
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****
Please use plain text.
Contributor
FedericoTech
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.

Please use plain text.
Contributor
FedericoTech
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?

Please use plain text.
New Contributor
klik
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'");

 

Please use plain text.
Developer
Zmey
Posts: 1,510
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

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. :smileyhappy:
Item { objectName: "button1" }

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


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

Re: getElementById in qml

yes, it works

 

thanks

Please use plain text.