Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

qt

qt5

qml

qtquick2

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?

like image 808
Ashif Avatar asked Feb 03 '16 13:02

Ashif


1 Answers

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.

like image 50
dtech Avatar answered Nov 15 '22 06:11

dtech