Home > database >  How to get Qt application window size from a QML file which is not the QML where the root window is
How to get Qt application window size from a QML file which is not the QML where the root window is

Time:03-29

While I am in a random QML file which is not the parent main.qml, is it possible to get the Qt application window size?

I know that I can declare the app window root in a global property like below and get the size anywhere.

ApplicationWindow {
    id: main_window_root
    visible: true
    width: 1000
    height: 800

    property alias main_window_root: main_window_root
}

But my primary question is, does Qt itself have a global property declared which can fetch the application window size?

I ask this because I see that I can get the application state, platform it is running on etc by doing a Qt. in any QML file. It looks like some properties are globally declared by Qt. Following is official documentation for this:
https://doc.qt.io/qt-5/qml-qtqml-qt.html

Hence the question, is the application window size also accessible through some global property in QML?

I am using Qt 5.15.8 commercial version.

CodePudding user response:

ApplicationWindow is a Window, which provides attached properties to read the width and height. So you can use Window.width and Window.height from anywhere to get the current window's dimensions. See docs here.

  • Related