Code review, look and feel

Please take a look at the following function. I am looking for suggestions on readability and functionality; your personal opinions are welcome. This function actually exist in a Qt QML script but is less of a C++ question and more of a for loops and if statements question. If you feel this is an inappropriate use of the beginners forums please say so and I will refrain from similar posts in the future.

//A function that can disable Qt virtualKeyboard keys by their text
function findChildByText(parent, objectText)
    var obj = null
    if (parent === null)
        return null
    var children = parent.children
    for (var i = 0; i < children.length; i++)
        obj = children[i]
        if (obj.text === objectText && obj.toString().substring(0, 7) === "BaseKey")
            obj.enabled = false
            obj.visible = false
        obj = findChildByText(obj, objectText)
        if (obj)
    return obj
Last edited on
This isn't actually C++ code I'm seeing.

So, I don't see that much can be said about it's C++ similarity.

C++ doesn't have "===", or "var", for example.

Beyond that, however, I do see several of the C++ coding standards used. I'm a strong believer in fully aligned braces (I do not favor Stroustrup formatting, one of the few disagreements I have with him).

Last edited on
You are right it is QML but this is less of a C++ question and more of a: use of for loops and if statements question. I will amend the original question.

EDIT: Actually, lets forget about it. It's an inappropriate use of your guy's time.
Last edited on
Well, maybe, since it isn't C++, but as for the loop:

This sequences through a container using the container's length, which relative to C++ is old style. Modern C++ would seek to use the newer "for" style, in which the container automatically provides it's length, a subtle but powerful way to assure we're not using the wrong "length".

Qt is from an older era, and catching up to modern C++. This is typical of all frameworks, and is in part why the language features incremental extensions (to absorb existing code).
Last edited on
I'm unfamiliar with QML (despite having used Qt as a library for a few projects in the past), but a few questions come to mind:

* Does QML not have a foreach (a.k.a. range-based for) loop?
* Is there a better test for the identity of obj than to see if its toString output starts with a certain prefix?
* Does QML have an equivalent of const, such that your children var could be made constant?


* You might want to come up with a better name for obj.
* If you care about your code being obvious, I feel like it's possible to restructure this code to make it more obvious as to what it returns. By inverting the condition on line 5 and moving your loop under that if statement, replacing the break with a return obj, and replacing the return obj with a return null, that would make its behavior easier to tell at a glance.

QML accepts JavaScript so I do have that at my disposal.

@Niccolo It seems you and Albatross agree. I will look and see if there is a similar way to represent the new C++ style for loops in QML/JavaScript


* I will look into it
* Unfortunately, the specific key I am looking for is the only key that was created as an object. I have an entirely different function for finding the other keys.
* No but JavaScript does, so I do.

*I changed the name from obj to theObject, is that better? I am terrible with these things. Perhaps I should call it keyObject?
*I am not sure I understand >.< move my loop under the if statement?

Thank you for the help guys.
Last edited on
Topic archived. No new replies allowed.