QML 튜토리얼 2 - QML 컴포넌트
이 장에서는 텍스트의 색상을 변경할 수 있는 색상 선택기를 추가합니다.

이 색상 선택기는 서로 다른 색상을 가진 6개의 셀로 구성되어 있습니다. 각 셀마다 동일한 코드를 반복해서 작성하는 것을 피하기 위해, 새로운 Cell 컴포넌트를 생성합니다. 컴포넌트는 다른 QML 파일에서 재사용할 수 있는 새로운 유형을 정의하는 방법을 제공합니다. QML 컴포넌트는 일종의 블랙박스처럼 작동하며, 속성(properties), 신호(signals), 함수(functions)를 통해 외부와 상호작용하며, 일반적으로 별도의 QML 파일에 정의됩니다. (자세한 내용은 Component 문서를 참조하십시오). 컴포넌트의 파일 이름은 반드시 대문자로 시작해야 합니다.
다음은 ` Cell.qml`에 대한 QML 코드입니다:
import QtQuick
Item {
id: container
property alias cellColor: rectangle.color
signal clicked(cellColor: color)
width: 40; height: 25
Rectangle {
id: rectangle
border.color: "white"
anchors.fill: parent
}
MouseArea {
anchors.fill: parent
onClicked: container.clicked(container.cellColor)
}
}실습
Cell 컴포넌트
Item {
id: container
property alias cellColor: rectangle.color
signal clicked(cellColor: color)
width: 40; height: 25이 컴포넌트의 루트 타입은 id 컨테이너를 가진 Item 입니다. Item 는 QML에서 가장 기본적인 시각적 타입이며, 종종 다른 타입을 담는 컨테이너로 사용됩니다.
property alias cellColor: rectangle.colorcellColor 속성을 선언합니다. 이 속성은 컴포넌트 외부에서도 접근할 수 있으므로, 서로 다른 색상으로 셀을 인스턴스화할 수 있습니다. 이 속성은 기존 속성(셀을 구성하는 사각형의 색상)에 대한 별칭일 뿐입니다( 속성 바인딩 참조).
signal clicked(cellColor: color)또한 이 컴포넌트에 color 유형의 cellColor 매개변수를 갖는 clicked라는 신호를 추가하고자 합니다. 나중에 메인 QML 파일에서 이 신호를 사용하여 텍스트의 색상을 변경할 것입니다.
Rectangle {
id: rectangle
border.color: "white"
anchors.fill: parent
}이 셀 컴포넌트는 기본적으로 ` id ` 속성을 가진 색상이 지정된 사각형입니다.
anchors.fill 속성은 시각적 요소의 크기를 설정하는 편리한 방법입니다. 이 경우 사각형은 부모 요소와 동일한 크기를 갖게 됩니다( 앵커 기반 레이아웃 참조).
MouseArea {
anchors.fill: parent
onClicked: container.clicked(container.cellColor)
}셀을 클릭했을 때 텍스트의 색상을 변경하기 위해, 부모 요소와 동일한 크기를 가진 ` MouseArea ` 유형을 생성합니다.
MouseArea 은 clicked라는 신호를 정의합니다. 이 신호가 트리거되면 색상을 매개변수로 하여 자체 clicked 신호를 발산하도록 합니다.
메인 QML 파일
메인 QML 파일에서 Cell 컴포넌트를 사용하여 색상 선택기를 만듭니다:
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
}
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 }
}
}그리드에 서로 다른 색상의 셀 6개를 배치하여 색상 선택기를 만듭니다.
Cell { cellColor: "red"; onClicked: helloText.color = cellColor }셀의 clicked 신호가 트리거되면, 매개변수로 전달된 cellColor 값을 텍스트 색상으로 설정하고자 합니다. 'onSignalName'이라는 이름의 속성을 통해 컴포넌트의 모든 신호에 반응할 수 있습니다( 신호 속성 참조).
© 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.