[QT]example2-QML(1)

๐Ÿ“ QT์‚ฌ์šฉ์‹œ ํ”„๋ก ํŠธ์—”๋“œ ๋””์ž์ธ์„ QT Creator์—์„œ widget์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.(๊ธฐ์กด ๋ฐฉ์‹)
When using QT, you can create front-end designs as widgets in QT Creator (existing method).

๐Ÿ“ QML์€ QT๋กœ ๋งŒ๋“ค ์•ฑ์˜ ๋””์ž์ธ์„ ๋งŒ๋“œ๋Š” ์ƒˆ๋กœ์šด ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
QML is a new way to create designs for apps built with QT.

๐Ÿ“ QT Design Studio์„ ์‚ฌ์šฉํ•˜๋ฉด ์ง์ ‘ ๋‹ค ์ฝ”๋”ฉํ•˜์ง€์•Š์•„๋„ QML Code๋ฅผ ์ƒ์„ฑ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
QT Design Studio lets you generate QML code without having to code it yourself.

-๐Ÿ“QT Design Studio๋Š” Qt Creator์„ค์น˜ํ•  ๋•Œ ์ด๋ฏธ ์„ค์น˜ํ–ˆ์Šต๋‹ˆ๋‹ค.
QT Design Studio was already installed when you installed Qt Creator.

๐Ÿ“ ์„ค์น˜ ์œ„์น˜๋Š” Qt Creator์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
The installation location is the same as Qt Creator.

๐Ÿ“1.QT Design Studio๋กœ ๋””์ž์ธ ๋งŒ๋“ค๊ธฐ / Create designs with QT Design Studio

โœ”๏ธQT Desing Studio ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ : Desktop ์„ ํƒํ•˜์…”์„œ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
Create a QT Design Studio project: Select Desktop and create a project.

โœ”๏ธ ์ขŒ์ธก ์ƒ๋‹จ์— LIve Preview์„ ํƒํ•˜์‹œ๊ณ  ์„ธ๋ชจ ์•„์ด์ฝ˜์„ ํด๋ฆญํ•˜์‹œ๋ฉด ํ”„๋ฆฌ๋ทฐ๋ฅผ ๋ณด์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
You can see the preview by selecting LIve Preview in the upper left corner and clicking the triangle icon.

โœ”๏ธ QT Desing Studio ์ขŒ์ธก ๋ฉ”๋‰ด์—์„œ ๊ทธ๋ƒฅ ํ™”๋ฉด์— ๋Œ์–ด๋„ ๋†“์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
You can simply drag and drop it onto the screen from the left menu in QT Design Studio.

โœ”๏ธ ํ™”๋ฉด ๊ตฌ์„ฑ์ด ์ž˜ ์•ˆ๋˜๋ฉด codeํƒญ์—์„œ ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ์ˆ˜์ •ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
If the screen configuration is not correct, you can directly edit the code in the code tab.

โœ”๏ธ 2D,Codeํƒญ์ด ์•ˆ๋ณด์ด๋ฉด ๋ฉ”๋‰ด์—์„œ VIew->Views์— ๋ณด์‹œ๋ฉด 2D,Code ์ฒดํฌ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
If you don’t see the 2D, Code tab, go to View->Views in the menu and check 2D, Code.

๐Ÿ“2.QT Creator์—์„œ QML ์ ์šฉํ•˜๊ธฐ / Applying QML in QT Creator

โœ”๏ธ QT Design Studio ํ”„๋กœ์ ํŠธ ํด๋”์— ๊ฐ€๋ณด์‹œ๋ฉด ์ €์˜ ๊ฒฝ์šฐ๋Š” ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
If you go to the QT Design Studio project folder, in my case it is created like this.

โœ”๏ธScreen01.ui.qml์ด ํŒŒ์ผ์„ QT Creator์—์„œ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
You can use this file Screen01.ui.qml in QT Creator.

โœ”๏ธ ์ด ํŒŒ์ผ์„ QT Creator์—์„œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ Screen01.ui.qml ์ฝ”๋“œ ์ˆ˜์ •์ด ํ•„์š” ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
If you use this file in QT Creator, you may need to modify the Screen01.ui.qml code.

โœ”๏ธ ์—ฌ๋Ÿฌ๋ถ„์˜ ํ™˜๊ฒฝ์— ๋งž๊ฒŒ ์ˆ˜์ •ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
You can modify it to suit your environment.


โœ”๏ธ1)QMLํŒŒ์ผ ๋ณต์‚ฌ ๋ฐ ๋””๋ ‰ํ† ๋ฆฌ ์ •๋ฆฌ / Copy QML files and organize directories
— ์œ„์˜ Screen01.ui.qmlํŒŒ์ผ์„ QT Creatorํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด์— qml๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ณ  ํŒŒ์ผ์„ ๋ถ™์—ฌ ๋„ฃ์Šต๋‹ˆ๋‹ค.
Create a qml directory within the QT Creator project directory and paste the reen01.ui.qml file.

— ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ณ  ํŒŒ์ผ์„ ๋ถ™์—ฌ๋„ฃ์–ด๋„ QT Creator์—์„œ๋Š” ๋ณด์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
Even if I create a directory and paste a file into it, it is not visible in QT Creator.

–QT Creator์— ๋ณด์ด๊ฒŒ ๋งŒ๋“ค๋ ค๋ฉด CmakeLists.txtํŒŒ์ผ์„ ์ˆด์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
You need to edit the CmakeLists.txt file to make it visible in QT Creator.

โœ”๏ธ2)CmakeLists.txt ํŒŒ์ผ ์ˆ˜์ • / Modify the CmakeLists.txt file

— CmakeLists.txt์— qmlํŒŒ์ผ์„ ์ธ์‹ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋‹ค์Œ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ ํ•ฉ๋‹ˆ๋‹ค.
Add the following to CmakeLists.txt to enable qml file recognition

— ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜๊ณ  ์ €์žฅํ•˜๋ฉด ํ”„๋กœ์ ํŠธ ์ฐฝ์— ํŒŒ์ผ์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.
When you edit and save a file, it will appear in the project window.

# qml ํŒŒ์ผ๋“ฑ๋ก / Registering qml file
qt_add_qml_module(appexample2
    URI example2
    VERSION 1.0
    QML_FILES
        Main.qml
        qml/Screen01.qml
)

3)Main.qmlํŒŒ์ผ ์ˆ˜์ • / Modify the Main.qml file

import QtQuick

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    // UI ํŒŒ์ผ ๋กœ๋”ฉ / UI file loading
    Loader {
        anchors.fill: parent
        source: "qml/Screen01.qml"
    }
}

๐Ÿ“3.์‹คํ–‰ / run

โœ”๏ธ ๋นŒ๋“œํ•˜๋ฉด ์ด๋ ‡๊ฒŒ ํ™”๋ฉด์— ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
When you build, you will see the program running on the screen like this.

๐Ÿ“4.์ „์ฒด์ฝ”๋“œ / Full Code

โœ”๏ธ qml/Screen01.qml



/*
This is a UI file (.ui.qml) that is intended to be edited in Qt Design Studio only.
It is supposed to be strictly declarative and only uses a subset of QML. If you edit
this file manually, you might introduce QML code that is not supported by Qt Design Studio.
Check out https://doc.qt.io/qtcreator/creator-quick-ui-forms.html for details on .ui.qml files.
*/
import QtQuick
import QtQuick.Controls

/*-- ํŒŒ์ผ์„ ์ธ์‹ ๋ชปํ•จ --*/
// Constants.qmlํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
//import "Settings.qml" as Constants

Rectangle {
    // id: rectangle
    // width: Constants.width
    // height: Constants.height

    // color: Constants.backgroundColor

    // ์ƒ์ˆ˜ ์ง์ ‘ ์„ ์–ธ
    property int widthConst: 1920
    property int heightConst: 1080
    property color backgroundColorConst: "#EAEAEA"

    width: widthConst
    height: heightConst
    color: backgroundColorConst

    Column {
        id: column
        width: 200
        anchors.centerIn: parent
        spacing: 10

        Rectangle {
            width: 180
            height: 30
            color: "white"
            border.color: "black"

            TextEdit {
                id: textEdit
                anchors.fill: parent
                width: 80
                height: 20
                text: qsTr("์ž…๋ ฅ1/input1")
                font.pixelSize: 12
            }
        }

        Rectangle {
            width: 180
            height: 30
            color: "white"
            border.color: "black"

            TextEdit {
                id: textEdit2
                width: 80
                height: 20
                text: qsTr("์ž…๋ ฅ2/input2")
                font.pixelSize: 12
            }
        }

        Text {
            id: text1
            text: qsTr("๊ฐ’์ถœ๋ ฅ/Print Value")
            font.pixelSize: 12
        }

        Button {
            id: button
            text: qsTr("Button")
        }
    }
}

โœ”๏ธ main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    QObject::connect(
        &engine,
        &QQmlApplicationEngine::objectCreationFailed,
        &app,
        []() { QCoreApplication::exit(-1); },
        Qt::QueuedConnection);
    engine.loadFromModule("example2", "Main");

    return app.exec();
}

โœ”๏ธ Main.qml

import QtQuick

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    // UI ํŒŒ์ผ ๋กœ๋”ฉ / UI file loading
    Loader {
        anchors.fill: parent
        source: "qml/Screen01.qml"
    }
}

Leave a Reply