[QT]Column,Row,ColumnLayout,RowLayout,anchor

๐Ÿ‘‰๐Ÿป QT QML์—์„œ ๊ฐ€๋กœ ์„ธ๋กœ ๋ ˆ์ด์•„์›ƒ์— ๋Œ€ํ•œ ์„ค๋ช…๊ณผ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
Description and code for horizontal and vertical layout in QT QML.

๐Ÿ‘‰๐Ÿป๊ฐ€๋กœ ,์„ธ๋กœ๋ ˆ์ด์•„์›ƒ์—๋Š” Column,Row,ColumnLayout,RowLayout์ด ์žˆ์Šต๋‹ˆ๋‹ค.
For horizontal and vertical layouts, there are Column, Row, ColumnLayout, and RowLayout.

๐Ÿ‘‰๐Ÿป ColumnLayout๊ณผ RowLayout์€ ๋ฐ˜์‘ํ˜• UI๋‚˜ ์ฐฝ ๋ฆฌ์‚ฌ์ด์ฆˆ ๋Œ€์‘ UI์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.
ColumnLayout and RowLayout are suitable for responsive UI or UI that responds to window resizing.

1.Colum {} : ์•„์ดํ…œ์„ ์„ธ๋กœ๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. / Used to align items vertically.

2.Row {} : ์•„์ดํ…œ์„ ๊ฐ€๋กœ๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค / Used to align items horizontally

3.ColumnLayout {} :

— Column๊ณผ ๊ฐ™์ด ์„ธ๋กœ๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. / Used to align vertically, like a column.

— ์ข€๋” ๋ณต์žกํ•œ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ• ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. / Used when using more complex structures.

4.RowLayout {} :

— Row์™€ ๊ฐ™์ด ๊ฐ€๋กœ๋กœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. / Used to align horizontally, like Row.

— ์ข€ ๋” ๋ณต์žกํ•ฉ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. / Used when using more complex structures.

6.anchor :

— QML์—์„œ UI ์š”์†Œ์˜ ์œ„์น˜๋ฅผ ๋‹ค๋ฅธ ์š”์†Œ(๋˜๋Š” ๋ถ€๋ชจ)์˜ ํŠน์ • ์œ„์น˜์— ๋งž์ถฐ์„œ ๋ฐฐ์น˜ํ•˜๊ธฐ ์œ„ํ•œ ์†์„ฑ ์‹œ์Šคํ…œ.
A property system in QML for positioning UI elements relative to a specific location in another element (or parent).

5.์ฝ”๋“œ / Code

โœ”๏ธMain.qml

— ์ฝ”๋“œ์—๋ณด์‹œ๋ฉด ListModel๊ณผ Repeater๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
If you look at the code, there is a ListModel and a Repeater.

— ListModel์€ ๋ฐฐ์—ด,Repeater๋Š” ๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์–ธ์–ด์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ˜๋ณต๋ฌธ์ด๋ผ๊ณ  ์ดํ•ดํ•˜์…”๋„ ๋ฉ๋‹ˆ๋‹ค.
You can understand ListModel as an array, and Repeater as a loop used in most programming languages.

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

ApplicationWindow {
    visible: true
    width: 800
    height: 600
    title: "Layout,ListModel"

    // ๋ฐ์ดํ„ฐ ๋ชจ๋ธ ์ •์˜
    ListModel {
        id: fruitModel
        ListElement { name: "์‚ฌ๊ณผ/Apple" }
        ListElement { name: "๋ฐ”๋‚˜๋‚˜/Banana" }
        ListElement { name: "ํฌ๋„/Grape" }
    }

    ListModel {
        id: gameModel
        ListElement { name : "Assassin's Creed" }
        ListElement { name : "Uncharted3" }
        ListElement { name : "Horizon zero dawn" }
    }

    // ํ™”๋ฉด์— ํ‘œ์‹œ
    Column {
        id:column1
        anchors.centerIn: parent // vertical,horizontal๋ชจ๋‘ ๊ฐ€์šด๋ฐ ์ •๋ ฌ / Center align both vertically and horizontally

        Repeater {
            model: fruitModel
            Text {
                text: model.name   // ListElement ์•ˆ์˜ name ์†์„ฑ ์‚ฌ์šฉ / Using the name property in ListElement
                font.pixelSize: 18
            }
        }
    }

// ํ™”๋ฉด์— ํ‘œ์‹œ
// anchors.centerIn: column1 : ๊ฐ€๋กœ,์„ธ๋กœ๋ฅผ ๋ชจ๋‘ ๊ฐ€์šด๋ฐ ์ •๋ ฌ ๋จ,์ž˜๋ชป ์‚ฌ์šฉํ• ๊ฒฝ์šฐ ์ถฉ๋Œ ์˜ค๋ฅ˜ ์žˆ์Œ.
// Display on screen
// anchors.centerIn: column1 : Centers both horizontally and vertically. If used incorrectly, a crash error may occur.

    Row {
        id:row1
        //anchors.centerIn: column1
        anchors.top: column1.bottom        // Column ๋ฐ”๋กœ ์•„๋ž˜ ์œ„์น˜ / Located just below the column
        anchors.horizontalCenter: parent.horizontalCenter  // ๊ฐ€๋กœ ๊ฐ€์šด๋ฐ ์ •๋ ฌ / horizontally center aligned
        anchors.topMargin: 20              // ์œ„์ชฝ ์—ฌ๋ฐฑ / top margin
        Repeater {
            model: fruitModel
            Text {
                text: model.name
                font.pixelSize :  18
            }
        }
    }

    ColumnLayout {
        id:column2
        anchors.top:row1.bottom
        anchors.horizontalCenter: row1.horizontalCenter  // row1์˜ ๊ฐ€๋กœ ์ค‘์‹ฌ์— ์ •๋ ฌ / Align to the horizontal center of row1
        anchors.topMargin: 20   
        Repeater {
            model: gameModel
            Text {
                text:model.name
                font.pixelSize : 18
            }
        }
    }

    RowLayout {
        id:row2
        anchors.top:column2.bottom
        anchors.horizontalCenter: row1.horizontalCenter  // row1์˜ ๊ฐ€๋กœ ์ค‘์‹ฌ์— ์ •๋ ฌ / Align to the horizontal center of row1
        anchors.topMargin: 20   
        Repeater {
            model: gameModel
            Text {
                text:model.name
                font.pixelSize : 18
            }
        }
    }
}

6.์Šคํฌ๋ฆฐ์ƒท / ScreenShot

Leave a Reply

Your email address will not be published. Required fields are marked *