[Interest] Accessing a QML object, via constructed id in JavaScript

Eric Feigenson eric at feigenson.net
Tue Apr 15 06:50:12 CEST 2014


Hello all...

I'm writing some JavaScript code inside QML.  What I'd like to do is be able to access an object via id where the id is in a JavaScript string.

For example, given the QML:

        CheckBox {
            id: english_weight1
            text: qsTr("2.5 lb")
        }

and the JavaScript fragment:

        var idName = "english_weight1";
        if (idName.checked)
            // do something is the box is checked...

But I want the idName in the "if" statement to refer to the value of the JavaScript variable "idName", in other words,
"english_weight1.checked". I believe I could use eval:

        if (eval(idname + ".checked")) ...

but everything I've read says eval is to be avoided.

If it helps or makes any difference, the CheckBox is in a Column with id myColumn.  I was thinking something like
myColumn[idName] would do the trick, but I'm only guessing.

I hope this is a clear enough description... please let me know if any more details are needed.

Any thoughts?

Thanks!

-Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140415/bb206ddf/attachment.html>


More information about the Interest mailing list