๐ ๋ฒํผ ํด๋ฆญ์ ๋ค์ด์ผ๋ก๊ทธ ์ฐฝ๊ณผ ์๋ก์ด ์ฐฝ์ ์คํํฉ๋๋ค.
When you click the button, a dialog window and a new window open.
๐ ์๋ก์ด ์ฐฝ์์ ํ
์คํธ ์
๋ ฅํ๊ณ ๋ฒํผ ํด๋ฆญ์ mainwindow์ ๋ผ๋ฒจ์ ํ
์คํธ๊ฐ ์ถ๋ ฅ๋ฉ๋๋ค.
Enter text in a new window and click the button to display the text in the label of the mainwindow.
๐ ์๋ก์ด์ฐฝ๊ณผ ์ด์ ์ฐฝ ์ฌ์ด์ ๋ณ์ ์ ๋ฌ์ ์๊ทธ๋๊ณผ ์ฌ๋กฏ์ ์ฌ์ฉํฉ๋๋ค.
Passing variables between new and old windows uses signals and slots.

Project WIndow
1.CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(example9 VERSION 0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 REQUIRED COMPONENTS Quick)
qt_standard_project_setup(REQUIRES 6.8)
qt_add_executable(appexample9
main.cpp
)
#-- qml ํ์ผ ์ถ๊ฐ / Add qml files --
qt_add_qml_module(appexample9
URI example9
VERSION 1.0
QML_FILES
Main.qml
qml/mainwindow.qml
qml/dialog.qml
qml/newwindow.qml
)
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(appexample9 PROPERTIES
# MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appexample9
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
target_link_libraries(appexample9
PRIVATE Qt6::Quick
)
include(GNUInstallDirs)
install(TARGETS appexample9
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
2.qml/dialog.qml
// dialog.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
Window {
id: dialogWindow
width: 400
height: 300
title: "Dialog Window"
visible: false
modality: Qt.ApplicationModal // ๋ชจ๋ฌ์ฒ๋ผ ๋์ / behaves like a modal
Rectangle {
anchors.fill: parent
color: "#f0f0f0" // ๋ฐฐ๊ฒฝ ์์ / background color
ColumnLayout {
anchors.fill: parent
spacing: 20
//padding: 20
Label {
text: "์ด๊ฒ์ ์ ์ฐฝ์
๋๋ค / This is a new window"
horizontalAlignment: Text.AlignHCenter
Layout.alignment: Qt.AlignHCenter
}
RowLayout {
Layout.alignment: Qt.AlignHCenter
spacing: 20
Button {
text: "ํ์ธ/Confirm"
Layout.preferredWidth: 80
onClicked: dialogWindow.close()
}
Button {
text: "์ทจ์/Cancle"
Layout.preferredWidth: 80
onClicked: dialogWindow.close()
}
}
}
}
}
3.qml/mainwindow.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
// ์ค์ ์์ ฏ / Central Widget
Rectangle {
anchors.fill: parent
color: "#f0f0f0"
// ํ
์คํธ ๋ผ๋ฒจ / Text Label
Label {
id: label
text: "TextLabel"
width: 221
height: 20
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
anchors.topMargin: 270
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
// ์ฒซ ๋ฒ์งธ ๋ฒํผ / First Button
Button {
id: pushButton
text: "PushButton"
width: 75
height: 24
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: label.bottom
anchors.topMargin: 30
onClicked: {
var component = Qt.createComponent("newwindow.qml")
if (component.status === Component.Ready) {
//== ์ฌ๋กฏ/Slot ==
var newWindowInstance = component.createObject(pushButton)
newWindowInstance.textSubmitted.connect(function(text) {
label.text = text
console.log = "Slot"
})
//====
newWindowInstance.visible = true
} else {
console.error("์ ์ฐฝ์ ๋ก๋ํ ์ ์์ต๋๋ค./The new window cannot be loaded.")
}
}
}
// ๋ ๋ฒ์งธ ๋ฒํผ / Second Button
Button {
id: pushButton2
text: "Dialogue Button2"
width: 131
height: 21
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: pushButton.bottom
anchors.topMargin: 16
onClicked: {
var component = Qt.createComponent("dialog.qml")
if (component.status === Component.Ready) {
var dialogInstance = component.createObject(pushButton2)
dialogInstance.visible = true
} else {
console.error("Dialog component failed to load")
}
}
}
// ์ํ๋ฐ (QML์์๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณต๋์ง๋ ์์ง๋ง, ์๋์ฒ๋ผ ๊ตฌํ ๊ฐ๋ฅ)
// Status bar (not provided by default in QML, but can be implemented as shown below)
ToolBar {
height: 22
width: parent.width
anchors.bottom: parent.bottom
RowLayout {
anchors.fill: parent
Label {
text: "StatusBar"
Layout.alignment: Qt.AlignLeft
}
}
}
}
4.qml/newwindow.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
Window {
id: newWindow
width: 400
height: 300
visible: true
title: "new window"
// ๋ผ๋ฒจ์ ์ถ๋ ฅํ๊ธฐ ์ํ ์๊ทธ๋ ์ ์
// Define a signal to output to the label
signal textSubmitted(string text)
Rectangle {
anchors.fill: parent
color: "#f0f0f0" // ๋ฐฐ๊ฒฝ์ปฌ๋ฌ / Background Color
ColumnLayout {
anchors.centerIn: parent
spacing: 10
TextField {
id: inputField
placeholderText: "ํ
์คํธ๋ฅผ ์
๋ ฅํ์ธ์ / Enter text"
Layout.fillWidth: true
}
Button {
id: confirmButton
text: "ํ์ธ / Confirm"
Layout.alignment: Qt.AlignHCenter
onClicked: {
// ์ฝ์์ ์ถ๋ ฅ
// output to console
console.log("์
๋ ฅ๋ ํ
์คํธ / Entered Text:", inputField.text)
//์๊ทธ๋ ๋ฐ์
//Signal generation
textSubmitted(inputField.text)
// ์ฐฝ ๋ซ๊ธฐ ์ํ ๊ฒฝ์ฐ / If you want to close the window : newWindow.close()
}
}
}
}
}
5.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("example9", "Main");
return app.exec();
}
6.Main.qml
import QtQuick
Window {
width: 850
height: 650
visible: true
title: qsTr("Main Window")
// ๋ก์ปฌ QML ํ์ผ ๋ก๋ / Local QML file load
Loader {
anchors.fill: parent
source: "qml/mainwindow.qml"
}
}
7.์คํ / Run
