0

So in QML, I can declare a stackview like this:

ApplicationWindow {
    Item{
        id: globalObject
        specialProperty: "xyz"
    }
    StackView{
        id: sv
    }
}

Then I have a button which loads a "Page1.qml" file into the StackView. When the program runs, I can access the globalObject from code in Page1.qml but qtCreator's autocomplete is oblivious to this connection. It is not very quick to go back and forth between Page1.qml and main.qml to find the name of special property especially when there are many objects that have to be shared between multiple pages.

In general, what I want to know, is there a way to statically link a response to a dynamically loaded object or leave a hint for the IDE that the following objects in this file are available to this other file?

  • 1
    Implicitly accessing an id in a parent context like that is not recommended and is frowned upon. Pass the globalObject as a property to your StackView children or make it a Singleton. – GrecKo Apr 22 '22 at 14:09
  • There are many pages that have to deal with the same object. It feels redundant to pass the same object again and again if you can easily define it as a parent that all children can access. I read into singletons but even that is frowned on [link](https://stackoverflow.com/questions/2551112/purpose-of-singletons-in-programming). – Khushal Khan Liwal Apr 28 '22 at 08:21
  • 1
    There is a difference between global singletons and singletons in the context of QML. And your globalObject is just a worse singleton with no advantage over it. Worse performance since it has to traverse your context hierarchy to find it, it can be easily shadowed, no type checking, no tooling support like you have seen, its origin is not clear when you use it in another file. – GrecKo Apr 28 '22 at 09:50

0 Answers0