[QT QML] ์•ต์ปค / Anchor

๐Ÿ‘‰๐Ÿป 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

Leave a Reply