Important: Please read the Qt Code of Conduct -

How to find nested QML objects for C++

  • So I have QML file with a StackView and a Rectangle and ListView nested inside. The Rectangle and ListView have signals that I want to connect in C++ but I am unsure of how to get them. I have a view in C++ and I tried doing view.findChild<QObject*>("objectName") but it comes back NULL. Each of the QML objects have a unique id and objectName assigned to them so I can't figure out what I am doing wrong. If someone could offer some suggestions, I would greatly appreciate it.

  • Moderators

    This is what the docs say:

    Delegates are instantiated as needed and may be destroyed at any time. They are parented to ListView's contentItem, not to the view itself. State should never be stored in a delegate.

    So, I think you should first find the content item and then run findChild() on that object.

  • @RyonMS Could you elaborate on your question? It is unclear to me what you are trying to do. You say you have a C++ view, which you want to connect to a Rectangle and ListView?

    Maybe it's a good idea to describe your use case. Does the user clicks on an item in the ListView and you want that item to appear more detailed somewhere else?

  • @sierdzio That did it! Thanks!

  • Moderators

    Nice! Happy coding!

Log in to reply