๐ ๋ฒํผ ํด๋ฆญ์ ๋ค์ด์ผ๋ก๊ทธ ์ฐฝ๊ณผ ์๋ก์ด ์ฐฝ์ ์คํํฉ๋๋ค.
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.
1.ํด๋์ค ๋ฐ ํผ ์ถ๊ฐ / Add classes and forms


2.ํผ๋์์ธ / Form Deisgn
โ๏ธ ์ข์ธก์ dialog.ui ์ค๋ฅธ์ชฝ์ mainwinodw.ui
On the left is dialog.ui and on the right is mainwinodw.ui


3.Header Files
โ๏ธ Dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
// ์์์ ํ๋ณํ๋ฐฉ์ง,์ค์ ๋ฐฉ์ง์ฉ
// To prevent implicit type conversion and mistakes
explicit Dialog(QWidget *parent = nullptr);
// ์๋ฉธ์:๊ฐ์ฒด ์ญ์ ์ ํธ์ถ
// Destructor: Called when the object is deleted
~Dialog();
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
โ๏ธ mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
// ์์ฐฝ ์ด๊ธฐ ์ฌ๋กฏ
// Open new window slot
void openNewWindow();
// Label์ถ๋ ฅ ์ฌ๋กฏ
// Label output slot
void updateLabel(const QString text);
// Dialog์ฌ๋กฏ
// Dialog slot
void openDialog();
};
#endif // MAINWINDOW_H
โ๏ธ newwindow.h
#ifndef NEWWINDOW_H
#define NEWWINDOW_H
// <QWidget์ ํค๋ํ์ผ์ ํฌํจ
#include <QWidget>
// ์ ๋ฐฉ ์ ์ธ(forward declaration)
// class <className> ์ด๋ฐ ํ์๋ง ์๋ ๊ฒฝ์ฐ ์ ๋ฐฉ ์ ์ธ์ด๋ผ๊ณ ํฉ๋๋ค.
// QLineEdit๊ณผ QPushButton์ ํฌ์ธํฐ๋ก๋ง ์ฌ์ฉํ ๊ฒฝ์ฐ, ์ ์ฒด ํค๋๋ฅผ ํฌํจํ์ง ์์๋ ๋ฉ๋๋ค.
// ์ด๋ ๊ฒ ํ๋ฉด ์ปดํ์ผ ์๋๊ฐ ๋นจ๋ผ์ง๊ณ , ์์กด์ฑ์ด ์ค์ด๋ญ๋๋ค.
// Forward declaration
// If the format is only class <className>, it is called a forward declaration.
// If you only use QLineEdit and QPushButton as pointers, you don't need to include the entire header.
// This speeds up compilation and reduces dependencies.
class QLineEdit;
class QPushButton;
class NewWindow : public QWidget
{
// ๋งคํฌ๋ก / macro
Q_OBJECT
public:
// ์์์ ํ๋ณํ ๊ธ์ง : ์ค์ ๋ฐฉ์ง, ์ฝ๋ ๋ช
ํ์ฑ, ์์ ํ ์์ฑ์ ํธ์ถ
// ์์์ ํ๋ณํ์ด๋ QT์์ฒด์์ ํ๋ผ๋ฉํฐ๋ก ๋์
๋๋ ๊ฐ์ ์๋์ผ๋ก ์ถ์ ํ๋ ๊ธฐ๋ฅ
// Implicit type conversions are prohibited: Preventing mistakes, improving code clarity, and ensuring safe constructor calls.
// Implicit type conversion is a feature of QT itself that automatically infers the values โโassigned to parameters.
explicit NewWindow(QWidget *parent = nullptr);
signals:
// ๋ฉ์ธ์ฐฝ์ผ๋ก ๋ณด๋ผ ์๊ทธ๋
// Signal to send to the main window
void textSubmitted(const QString &text);
private:
QLineEdit* inputField;
QPushButton* confirmButton;
};
#endif // NEWWINDOW_H
4. Source FIles
โ๏ธ dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);
}
// ์๋ฉธ์
// destructor
Dialog::~Dialog()
{
delete ui;
}
โ๏ธ main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
// -- ์ฝ๋ ์์ ์์ / No code modification --
โ๏ธ mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
// ์ ์ฐฝ ํด๋์ค ํฌํจ / Include a new window class
#include "newwindow.h"
// dialog ํด๋์ค ํฌํจ / Includes dialog class
#include "dialog.h"
// cout๊ณผ ์ ์ฌํ ๋๋ฒ๊น
ํด๋์ค / Debugging class similar to cout
#include <QDebug>
// ์๋ฐ์คํฌ๋ฆฝํธ alert๊ณผ ์ ์ฌํ ๋๋ฒ๊น
ํด๋์ค
// A debugging class similar to JavaScript Alert
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// ๋ฒํผ ํด๋ฆญ ์ ์ ์ฐฝ ์ด๊ธฐ
// Open a new window when the button is clicked
connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::openNewWindow);
// ๋ฒํผ ํด๋ฆญ์ ๋ค์ด์ผ๋ก๊ทธ ๋์ฐ๊ธฐ
// Pop up a dialog when a button is clicked
connect(ui->pushButton_2, &QPushButton::clicked, this, &MainWindow::openDialog);
}
// ์์ฐฝ ๋์ฐ๋ ์ฌ๋กฏ ํจ์
// Slot function to open a new window
void MainWindow::openNewWindow(){
// === ๋๋ฒ๊น
/ Debugging ===
qDebug() << "openNewWindow ์ฌ๋กฏ ์คํ๋จ/slot executed!";
//QMessageBox::information(this, "ํ
์คํธ/test", "์ฌ๋กฏ์ด ์คํ๋์์ต๋๋ค/The slot has been run.!");
// ๋ถ๋ชจ ์์ด ๋
๋ฆฝ ์ฐฝ(๋๋ฒ๊น
)
// Independent window without parent (debugging)
//NewWindow* window = new NewWindow(nullptr);
NewWindow* window = new NewWindow(this);
// ๋
๋ฆฝ๋ ์ฐฝ์ผ๋ก ์ค์
// Set as independent window
window->setWindowFlags(Qt::Window);
// ์๊ทธ๋๊ณผ ์ฌ๋กฏ ์ฐ๊ฒฐ(๋ผ๋ฒจ ์ถ๋ ฅ์ฉ)
// Connecting signals and slots (for label output)
connect(window, &NewWindow::textSubmitted, this, &MainWindow::updateLabel);
// ๋ซ์ ๋ ๋ฉ๋ชจ๋ฆฌ ์๋ ํด์
// Automatically free memory when closing
window->setAttribute(Qt::WA_DeleteOnClose);
window->show();
}
// MainWindow์ ๋ผ๋ฒจ์ ํ
์คํธ ์ถ๋ ฅํ๋ ์ฌ๋กฏ ํจ์
// Slot function that outputs text to the label of MainWindow
// newwindow.cpp์์ ์๊ทธ๋ ๋ฐ์
// Signal generated in newwindow.cpp
void MainWindow::updateLabel(const QString text){
// ๋ผ๋ฒจ์ ํ
์คํธ ์ถ๋ ฅ
// Print text to label
ui->label->setText(text);
}
// ๋ค์ด์ผ๋ก๊ทธ ์ฐฝ ๋์ฐ๋ ์ฌ๋กฏ ํจ์
// Slot function to open a dialog window
void MainWindow::openDialog()
{
Dialog* dialog = new Dialog(this);
// ๋ค์ด์ผ๋ก๊ทธ ์ฐฝ์ผ๋ก ์ค์
// Set to dialog window
dialog->setWindowFlags(Qt::Dialog);
// ๋ซ์๋ ๋ฉ๋ชจ๋ฆฌํด์
// Free memory when closing
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->show();
}
// ์๋ฉธ์ / destructor
// ๊ฐ์ฒด๊ฐ ์ญ์ ๋๊ฑฐ๋ ๋ฒ์๋ฅผ ๋ฒ์ด๋ ๋ ์๋์ผ๋ก ํธ์ถ๋๋ ํจ์์
๋๋ค.
// A function that is automatically called when an object is deleted or goes out of scope.
MainWindow::~MainWindow()
{
delete ui;
}
.โ๏ธ newwindow.cpp
#include "newwindow.h"
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QDebug>
NewWindow::NewWindow(QWidget *parent) : QWidget(parent)
{
setWindowTitle("new window");
resize(400,300);
// ui์ถ๊ฐ,ui๋์์ธ ํ์ผ ์๋ ๊ฒฝ์ฐ.
// Add UI, if there is no UI design file.
inputField = new QLineEdit(this);
confirmButton = new QPushButton("ํ์ธ/Confirm",this);
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(inputField);
layout->addWidget(confirmButton);
//&QPushButton::clicked ์ด ์๊ทธ๋ ๋ฐ์์ [this]()์ฌ๋กฏ ํจ์ ์คํ
//&QPushButton::clicked When this signal occurs, the [this]() slot function is executed.
connect(confirmButton, &QPushButton::clicked, this, [this](){
QString text = inputField->text();
emit textSubmitted(text); // ์๊ทธ๋ ๋ฐ์
qDebug() << "์
๋ ฅ๋ ํ
์คํธ/Enterd TExt:" << text;
//close();// ์ฐฝ๋ซ๊ธฐ / Close window
});
}
5.์คํ / Run
