
Item 계열 컴포넌트
Item
Rectangle
Text
Image
Button
ListView
Slider
Switch
TextField
ProgressBar
ScrollView
ScrollView {
id: root
...
Component.onCompleted: {
root.contentItem.boundsBehavior = Flickable.StopAtBounds
}
}
버전에 따라 지원되지 않는 경우도 있다는데, 그 때는 아래 방법을 사용해본다.
ScrollView {
...
Binding {
target: scrollView.contentItem
property: "boundsBehaviour"
value: Flickable.StopAtBounds
}
}
Window 계열 컴포넌트
Window
Window {
visible: true
width: 300
height: 2200
title: "Custom Window"
flags: Qt.FramelessWindowHint |
Qt.WindowStaysOnTopHint
Text {
text: "This window has custom flags!"
anchors.centerIn: parent
}
}
ApplicationWindow
ApplicationWindow {
visible: true
width: 640
height: 480
title: "My Application"
Text {
text: "Hello, World!"
anchors.centerIn: parent
}
}
Tooltip
Button {
text: "Hover me"
// 마우스 커서가 버튼 위에 올라와 있는 상태일 때 Tooltip 표시
ToolTip.visible: hovered
ToolTip.text: "This is a tooltip!"
}
Popup
Popup {
id: myPopup
width: 200
height: 200
modal: true
focus: true
Rectangle {
color: "white"
border.color: "black"
anchors.fill: parent
Text {
text: "This is a popup"
anchors.centerIn: parent
}
}
}
Button {
text: "Show Popup"
onClicked: myPopup.open()
}
Position
Manual Position
Anchor
한 요소의 한 측면(상/하/좌/우)을 다른 요소의 특정 측면에 고정함

anchors.centerIn을 통해 요소의 정가운데에 위치 가능
anchors {
horizontalCenter : parent.horizontalCenter
verticalCenter: parent.verticalCenter
}
anchors.centerIn : parent //정 가운데
anchors.fill: parent //부모의 요소를 가득 채움
Column / Row
Flow
Flow {
anchors.fill: parent
spacing: 10
width: 300
}
GridLayout
GridLayout {
anchors.fill: parent
columns: 3
//default Spacing값 = 5
rowSpacing: 10
columnSpacing: 10
//첫 번째 행
Rectangle {
color: "red"
width: 100
height: 100
Layout.row: 0
Layout.column: 0
}
Rectangle {
color: "green"
width: 100
height: 100
Layout.row: 0
Layout.column: 1
}
Rectangle {
color: "blue"
width: 100
height: 100
Layout.row: 0
Layout.column: 2
}
//두 번째 행
Rectangle {
color: "black"
width: 100
height: 100
Layout.row: 1
Layout.column: 0
}
Rectangle {
color: "lightpink"
width: 100
height: 100
Layout.row: 1
Layout.column: 1
}
Rectangle {
color: "lightblue"
width: 100
height: 100
Layout.row: 1
Layout.column: 2
}
}
StackLayout
StackLayout {
id: stackLayout
width: 300
height: 300
Rectangle {
color: "lightblue"
width: parent.width
height: parent.height
}
Rectangle {
color: "yellow"
width: parent.width
height: parent.height
}
}
Button {
text: "Newt Page"
onClicked: {
stackLayout.currentIndex =
(stackLayout.currentIndex + 1) % stackLayout.children.length
}
}
MouseArea
마우스 Area에 클릭/커서 위치 변경/드래그 등 다양한 마우스 이벤트를 캡쳐해서 행동 생성 가능
각종 이벤트
MouseArea
{
anchors.fill: parent
onClicked: {
console.log("Mouse Clicked at: ", mouse.x, ".", mouse.y)
}
onDoubleClicked: {
}
onEntered: {
}
onExited: {
}
onPositionChanged: {
}
}
Rectangle {
id: draggableRect
width: 100
height: 100
color: "lightpink"
x: 10
y: 10
z: 1
MouseArea
{
id: dragArea
anchors.fill: parent
drag.target: draggableRect
onReleased: {
if (dropRect.contains(mapToItem(dropRect, dragArea.mouseX, dragArea.mouseY)))
{
dropRect.color = "black"
dropRect.name = "Dropped Here"
}
}
}
}
Rectangle {
id: dropRect
width: 200
height: 200
color: "lightgray"
anchors.centerIn: parent
property string name: "Drop Here"
Text {
id: dropText
text: dropRect.name
color: "green"
anchors.centerIn: parent
}
}
TapHandler
Rectangle {
width: 100
height: 100
color: "lightpink"
TapHandler {
onTapped: console.log("Rect Tappedd");
onDoubleTapped: console.log("Rect double Tapped");
onLongPressed: console.log("Rect long Pressed");
}
}
HoverHandler
Rectangle {
width: 100
height: 100
color: "lightpink"
Text {
id: rectText
text: "Hover me"
anchors.fill: parent
font.pointSize: 14
}
HoverHandler {
id: hoverHandler
acceptedDevices: PointerDevice.Mouse
cursorShape: Qt.PointingHandCursor
}
}
QML Function
Rectangle {
id: rectangle
//width: 200
//height: 200
anchors.fill: parent
//vanila js 문법 사용
function calculateArea(width, height)
{
return width * height
}
Text {
id: areaText
//property에 함수 바인딩
text: "Area: " + rectangle.calculateArea(rectangle.width, rectangle.height)
}
}
/* mathFunctions.js */
.pragma library
function add(x, y) {
return x+y;
}
function multiply(x, y) {
return x*y;
}
/* main.qml */
import "mathFunctions.js" as MathFunc //as 별칭은 대문자로 시작
Window {
width: 640
height: 480
visible: true
title: qsTr("Example")
Item {
Component.onCompleted: {
// 해당하는 UI 컴포넌트가 생성되었을 때, 수행할 액션 정의
console.log("3+5 =", MathFunc.add(3, 5))
console.log("3*5 =", MathFunc.multiply(3, 5))
}
}
}
Signal ↔ Slot
Signal: 어떤 이벤트가 발생했을 때 발송되는 메세지
Slot: Signal을 수신하여 특정 동작을 수행하는 함수
1Depth 예시
Item {
anchors.fill: parent
Rectangle {
width: 200; height: 100;
color: "lightblue"
signal buttonClicked(string msg) // msg : string
onButtonClicked: (msg) => {
console.log(msg)
}
Button {
text: "Click me"
anchors.centerIn: parent
onClicked: {
parent.buttonClicked("Button was clicked")
}
}
}
}
Item {
anchors.fill: parent
Rectangle {
id: parentRect
width: 300; height: 200;
color: "lightblue"
signal parentSignal(string msg)
onParentSignal: (msg) => {
console.log("Received in parent:", msg) // 4) 부모 시그널 수신
}
Rectangle {
id: childRect
width: 200; height: 100
color: "lightpink"
anchors.centerIn: parent
signal childSignal(string msg)
onChildSignal: (msg) => {
console.log("Received in child:", msg) // 2) 자식 시그널 수신
parentRect.parentSignal("Parent received child's signal") // 3) 부모 시그널 발생
}
MouseArea {
anchors.fill: parent
onClicked: childRect.childSignal("Child was clicked") // 1) 자식 시그널 발생
}
}
}
}
Custom QML Component
/* CustomButton.qml */
import QtQuick 2.15
Rectangle {
id: root
width: 100
height: 40
color: "lightpink"
radius: 10
signal clicked() //onClicked 동작 순서 (2)
Text {
text: "Click Button"
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: { //onClicked 동작 순서 (1)
root.clicked()
root.color = "cyan"
}
}
}
/* main.qml */
CustomButton {
anchors.centerIn: parent
onClicked: { //onClicked 동작 순서 (3)
console.log("Button Clicked")
}
}
/* AdvancedButton.qml */
Rectangle {
id: root
width: 100; height: 40
color: "lightpink"
radius: 4
border.color: "black"
property string label: "Clicked Me"
Text {
text: root.label
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: root.state = ((root.state === "pressed") ? "default" : "pressed")
}
state: "default"
states: [
State {
name: "default"
PropertyChanges {
target: root
color: "lightpink"
border.color: "black"
}
},
State {
name: "pressed"
PropertyChanges {
target: root
color: "black"
border.color: "yellow"
}
}
]
}
//Animation Transition
transitions: [
Transition {
from: "default"
to: "pressed"
ColorAnimation {
target: root
property: "color"
duration: 200
}
},
Transition {
from: "pressed"
to: "default"
ColorAnimation {
target: root
property: "color"
duration: 100
}
}
]