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)
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)
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).
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).
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.
* 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.
*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?