Ashif
Ashif

Reputation: 1684

How to access "ApplicationWindow" reference in other QML file?

I have a file called main.qml, which instantiate ApplicationWindow. I would like to access this object(app) in other qml file.

ApplicationWindow {
    id:app
    title: "Title"
    visible: true
    property int keyboardPosition:10//need to access from other qml files for set/get
....
...
}

Is there any API available to access qml application object?
or How to set application settings in qml. is it singleton class approach or any other?

Upvotes: 6

Views: 4135

Answers (1)

dtech
dtech

Reputation: 49329

ApplicationWindow {
   id: app
   property ApplicationWindow appWindow : app
}

Then appWindow will be available to every object nested in the window in the object tree, because of dynamic scoping. So you can appWindow.keyboardPosition from anywhere. Note that if all you need to access is keyboardPosition - then you can do that from anywhere as well, as long as it is not overshadowed by another property with the same name up the object tree, you don't need to expose the window object as a property itself.

If you have multiple windows, the property will automatically resolve to the window the current object is in.

Upvotes: 10

Related Questions