Saturday, July 20, 2013

qml example: Layout in parent's center

Example show how to place something (Text and Image in this example) in center of parent:

Text and Image in center
Text and Image in center


import QtQuick 2.0

Rectangle {
    width: 360
    height: 360
    Text {
        text: qsTr("Hello World: " + parent.width + " x " + parent.height)
        anchors.centerIn: parent
    }
    Image {
        id: myimage
        source: "http://goo.gl/J4xj0"
        mirror: mouseArea.containsMouse ? true : false
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        MouseArea {
            id: mouseArea
            anchors.fill: parent
            hoverEnabled: true
            onClicked: Qt.quit()
        }
    }

}


reference: http://qt-project.org/doc/qt-4.8/qml-anchor-layout.html