Monday, July 15, 2013

Get window size of Qt Quick 2 Application, and handle size changed.

This example of QML show how to get width and height of window, and implement handle of onWidthChanged and onHeightChanged, which will be called automatically once window width or height changed.

import QtQuick 2.0

Rectangle {
    width: 360
    height: 360
    Text {
        text: qsTr("Hello World: " + parent.width + " x " + parent.height)
        anchors.centerIn: parent
    }
    onWidthChanged: print("onWidthChanged: " + width + " x " + height)
    onHeightChanged: print("onHeightChanged: " + width + " x " + height)
    MouseArea {
        anchors.fill: parent
        onClicked: {
            Qt.quit();
        }
    }
}