이 페이지에서

QML 튜토리얼 3 - 상태와 전환

이 장에서는 상태와 전환을 도입하여 이 예제를 조금 더 역동적으로 만들어 보겠습니다.

텍스트를 클릭하면 화면 하단으로 이동하고, 회전하며, 빨간색으로 변하도록 만들 것입니다.

다음은 QML 코드입니다:

import QtQuick

Rectangle {
    id: page
    width: 320; height: 480
    color: "lightgray"

    Text {
        id: helloText
        text: "Hello world!"
        y: 30
        anchors.horizontalCenter: page.horizontalCenter
        font.pointSize: 24; font.bold: true

        MouseArea { id: mouseArea; anchors.fill: parent }

        states: State {
            name: "down"; when: mouseArea.pressed == true
            PropertyChanges {
                helloText {
                    y: 160
                    rotation: 180
                    color: "red"
                }
            }
        }

        transitions: Transition {
            from: ""; to: "down"; reversible: true
            ParallelAnimation {
                NumberAnimation { properties: "y,rotation"; duration: 500; easing.type: Easing.InOutQuad }
                ColorAnimation { duration: 500 }
            }
        }
    }

    Grid {
        id: colorPicker
        x: 4; anchors.bottom: page.bottom; anchors.bottomMargin: 4
        rows: 2; columns: 3; spacing: 3

        Cell { cellColor: "red"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "green"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "blue"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "yellow"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "steelblue"; onClicked: helloText.color = cellColor }
        Cell { cellColor: "black"; onClicked: helloText.color = cellColor }
    }
}

실습

        states: State {
            name: "down"; when: mouseArea.pressed == true
            PropertyChanges {
                helloText {
                    y: 160
                    rotation: 180
                    color: "red"
                }
            }
        }

먼저, 텍스트 유형에 대한 새로운 ‘down’ 상태를 생성합니다. 이 상태는 MouseArea 가 눌렸을 때 활성화되고, 손을 떼면 비활성화됩니다.

'down' 상태에는 암시적 기본 상태 (QML에서 처음 정의된 항목)에서 변경된 일련의 속성들이 포함됩니다. 구체적으로, 텍스트의 ' y ' 속성을 ' 160'로, 'rotation'을 ' 180 '로, ' color '을 빨간색으로 설정합니다.

        transitions: Transition {
            from: ""; to: "down"; reversible: true
            ParallelAnimation {
                NumberAnimation { properties: "y,rotation"; duration: 500; easing.type: Easing.InOutQuad }
                ColorAnimation { duration: 500 }
            }
        }

텍스트가 즉시 하단에 나타나기보다는 부드럽게 이동하도록 하기 위해, 두 상태 사이에 전환 효과를 추가합니다.

from to 는 전환이 실행될 두 상태 사이의 구간을 정의합니다. 이 경우, 기본 상태에서 아래로 내려간 상태로의 전환을 원합니다.

다운 상태에서 기본 상태로 되돌아갈 때 동일한 전환이 역순으로 실행되도록 하려면, reversible 을 true 로 설정합니다. 이는 두 가지 전환을 따로 작성하는 것과 동일합니다.

ParallelAnimation 유형은 두 가지 유형의 애니메이션(숫자 및 색상)이 동시에 시작되도록 보장합니다. 대신 SequentialAnimation 를 사용하여 두 애니메이션을 차례로 실행할 수도 있습니다.

상태와 전환에 대한 자세한 내용은 Qt Quick States 및 상태와 전환 예제를 참조하십시오.

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.