๐๐ป QT QML์์ ์ต์ปค์ ๋ํ ์ค๋ช
์
๋๋ค.
Here is a description of anchors in QT QML.
๐๐ป QML์์ ๊ฐ ์์๋ฅผ ๋ฐฐ์นํ๊ธฐ ์ํด์ ๋ ์ด์์๊ณผ ์ต์ปค๋ฅผ ์ฌ์ฉ ํ ์ ์์ต๋๋ค.
You can use layouts and anchors to position each element in QML.
๐๐ป UI ์์๋ผ๋ฆฌ ๊ด๊ณ ๊ธฐ๋ฐ์ผ๋ก ๋ฐฐ์นํ ๊ฒฝ์ฐ์ ์ฌ์ฉํฉ๋๋ค.
Used when arranging UI elements based on their relationships.
๐๐ป์ต์ปค๋ ์๋์ ๊ฐ์ ์์ฑ๋ค์ด ์์ต๋๋ค.
Anchors have the following properties:
| ์์ฑ / Attribute | ์ค๋ช / Explanation |
|---|---|
anchors.left | ์ผ์ชฝ ์ ๋ ฌ / left align |
anchors.right | ์ค๋ฅธ์ชฝ ์ ๋ ฌ / right align |
anchors.top | ์์ชฝ ์ ๋ ฌ / top alignment |
anchors.bottom | ์๋์ชฝ ์ ๋ ฌ / bottom alignment |
anchors.horizontalCenter | ์ํ ์ค์ ์ ๋ ฌ / horizontal center alignment |
anchors.verticalCenter | ์์ง ์ค์ ์ ๋ ฌ / vertical center alignment |
anchors.centerIn | ๊ฐ๋ก+์ธ๋ก ์ ์ฒด ์ค์ ์ ๋ ฌ / Center all horizontal and vertical alignment |
anchors.fill | ๊ฝ ์ฑ์ฐ๊ธฐ / fill up |
๐๐ป ์ฌ์ฉ๋ฒ / Usage
โ๏ธ ์์ด๋.์์ฑ / id.attribute
— ์๋์ ์ฝ๋์ฒ๋ผ id๋ ๊ฐ ์์๋ฅผ ๊ตฌ๋ณํ๊ธฐ ์ํ ์ด๋ฆ์ผ๋ก id:”์ด๋ฆ”์ผ๋ก ์ค์ ํ ์ ์์ต๋๋ค.
As in the code below, id can be set as id:”name” to distinguish each element.
— ๋น์ฐํ id์ด๋ฆ์ ์ค๋ณต๋์ด์๋ ์๋๋ฉฐ ๊ณ ์ ํด์ผ ํฉ๋๋ค.
Of course, the ID name must not be duplicated and must be unique.
Text {
id:anchorTxt1
text: "Anchor1!"
anchors.centerIn: parent
font.pixelSize: 24
color: "black"
}
— ์์ ์์ด๋๋ฅผ ์ค์ ๋ก๋ ์๋์ฒ๋ผ ์ฌ์ฉํฉ๋๋ค.
The ID above is actually used as follows:
anchors.top : anchorTxt1.bottom
โ๏ธ๋ถ๋ชจ์์ดํ .์์ฑ / parent.attribute
— QML์์ parent๋ ํ์ฌ ์์๋ฅผ ๊ฐ์ธ๊ณ ์๋ ๋ฐ๋ก ์ ์์๋ฅผ ์๋ฏธํฉ๋๋ค.
In QML, parent means the element immediately above the current element.
— ์๋์ ์ฝ๋์์ parent๋ Rectangle์ id:rt๋ฅผ ์๋ฏธํฉ๋๋ค.
In the code below, parent refers to the id:rt of the Rectangle.
Text {
id:anchorTxt2
text: "Anchor2!"
anchors.left: parent.left
anchors.top: parent.top
font.pixelSize: 24
color: "black"
}
๐๐ป ์ฃผ์ ์ฌํญ / Caution
โ๏ธanchors.horizontalCenter์ anchors.verticalCenter๋ฅผ ์ค์ ์ ๋ ฌ ์ํค๋ฉด
anchors.centerIn๊ณผ ๊ธฐ๋ฅ์ด ๊ฐ์ต๋๋ค.
Centering with anchors.horizontalCenter and anchors.verticalCenter has the same functionality as anchors.centerIn.
โ๏ธ๊ทธ๋ฐ๋ฐ anchors.centerIn์ด ์ ์ฉ๋ UI์์์ anchors.horizontalCenter์ anchors.verticalCenter๋ฅผ ์ ์ฉํ๋ฉด ์์ฑ ์ถฉ๋์ด ์ผ์ด๋์ UI์์๊ฐ ์ ๋๋ก๋ฐฐ์น ๋์ง ์์ ์ ์์ต๋๋ค.
However, if you apply anchors.horizontalCenter and anchors.verticalCenter to a UI element to which anchors.centerIn has been applied, a property conflict may occur, causing the UI element to not be positioned properly.
โ๏ธ ์์ฑ ์ถฉ๋์ด ๋ฐ์ํ์ง ์๊ฒ ์์ ์ฌํญ์ ๊ณ ๋ คํด์ ์ฌ์ฉํ๋๊ฒ ์ค์ํฉ๋๋ค.
It is important to use it while considering the above to avoid attribute conflicts.
๐๐ป ์ฝ๋ / Code
import QtQuick
Window {
width: 640
height: 480
visible: true
title: qsTr("Anchor") // window title
Rectangle {
id:rt
color: "lightgreen"
// ๋ถ๋ชจ(Window)์ ๊ฝ ์ฑ์ฐ๊ธฐ
// QML์์ parent๋ ํ์ฌ ์์๋ฅผ ๊ฐ์ธ๊ณ ์๋ ๋ฐ๋ก ์ ์์
// Fill the parent (Window)
// In QML, parent is the element immediately above the current element.
anchors.fill: parent
// ๊ฐ์ด๋ฐ ์ ๋ ฌ / center aligned
Text {
id:anchorTxt1
text: "Anchor1!"
anchors.centerIn: parent
font.pixelSize: 24
color: "black"
}
// ์ข์๋จ / Top-Left
Text {
id:anchorTxt2
text: "Anchor2!"
anchors.left: parent.left
anchors.top: parent.top
font.pixelSize: 24
color: "black"
}
// ์ฐ์๋จ / Top-Right
Text {
id:anchorTxt3
text: "Anchor3!"
anchors.right:parent.right
anchors.top:parent.top
font.pixelSize: 24
color: "black"
}
// ์ขํ๋จ / Bottom-Left
Text {
id:anchorTxt4
text:"Anchors4!"
anchors.left:parent.left
anchors.bottom:parent.bottom
font.pixelSize: 24
color: "black"
}
// ์ฐํ๋จ / Bottom-Right
Text {
id:anchorTxt5
text:"Anchor5!"
anchors.right:rt.right // id
anchors.bottom:parent.bottom
font.pixelSize: 24
color: "black"
}
Text {
id:anchorTxt6 // id
text:"Anchor6!"
anchors.top : anchorTxt1.bottom
// horizontalCenter : ์ํ์ค์ฌ์ / horizontal center line
// verticalCenter : ์์ง ์ค์ฌ์ / vertical center line
// Anchor1 ๊ธฐ์ค ์ค์ ์ ๋ ฌ / Center alignment based on Anchor1
anchors.horizontalCenter: anchorTxt1.horizontalCenter
font.pixelSize:24
color:"black"
}
// Anchor6์ ๊ธฐ์ค์ Rectangle์ด ์๋๋ผ Anchor1 ํ
์คํธ(anchorTxt1)
// The reference for Anchor6 is not Rectangle, but Anchor1 text (anchorTxt1)
Text {
id:anchorTxt7
text:"Anchor7!"
anchors.top : anchorTxt6.bottom
anchors.left : anchorTxt6.right
font.pixelSize:24
color:"black"
}
}
}
๐๐ป์คํฌ๋ฆฐ์ท/ScreenShot
