4

How to find object like in C++ with findChild(), but from javascript?

0

1 Answer 1

0

As on the QML-side you usually have no access to the parent/child-relationships of the Qt/C++-side, but only to the visual parent/child-relationship, you will need to resort to C++.

You could e.g. create a object, that exposes a method which takes a QObject, and a name and calls the function findChild of this object.

If you only want to find a visual child, you might just implement a recursive bfs over the visual tree in JS and call this.

But as I said in my comment: If you need this, you probably messed up at some other place and should rather think about a way to do it, without findChild(). Using it is not recommended in C++, and is certainly not so in QML.
As it is a recursive search it will do its best in killing your performance. A UI contains easily a thousand elements and you would need to compare the strings all the time. Further you would lie about the dependencies by sneakingly access things you don't tell anyone you are depending on.

The objectName and the object that calls findChild might not be in the same logical part of your code, so it is easily broken, if someone might change this object or the objectName, and you are searching for a name that does not exist anymore.

Additionally, if you found an object with the name - and possibly the right type - there is still no guarantee, it is the right object, as the objectNames are not necessarily unique.

All in all, it is not the best design, to access objects like that - tough it might be possible.

Disclaimer: I have not tried out my proposed solution, as I don't want to waste my time on it.

Sign up to request clarification or add additional context in comments.

5 Comments

I want to change text property in the 'text' component. I have the following JSON [{"id":"text1", "newText":"Pizza"},{"id":"text1", "newText":"Ramen"}]. Where Id is the objectName and id of the text component. How to get text with this id?
I managed to do that by writing my own function getObjectByName(name). Fortunately, all my objects were in a container so I could do a linear search with itemAt(i).objectName and contentChildren. Is it the right way to do that?
"If you need this, you probably messed up at some other place and should rather think about a way to do it" I don't understand what is the issue with accessing elements programatically. This is one of the most basic things in software development. If you have let's say, 50 buttons, each one doing something similar, you might want to access them programatically in a for loop, etc. I think it's very narrow-minded and opinionated to say "if you're doing this way, you are probably doing it wrong"
@Bersan, what is the "correct" way largly depends on the programming language. There are conceptual differences between procedural, functional and declarative styles. Though procedural and functional programming is supported via the JS integration, it is discouraged to use it for more than the simplest tasks. Basic things in software development are: solving problems in the style the programming language at hand proposes it. Doing things in a certain way regardless of the style, just because it is somehow possible, I'd consider the hackers way. However Qt (not QML) allows for more, using C++.
I still don't exactly know what Usmich was attempting. Most likely the correct way would be the cration of a ViewModel to which the View-Component is bound. But the details are very much dependent on the (more) concrete usecase. Just as general advice for a evolvable user interface (in general): Avoid assuming structures and relationships as much as possible. Don't search when you want to update, rather register when you want to be updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.