๐ ๋ฒํผ ํด๋ฆญ ํ ๊ฒฝ์ฐ ์ ์ฐฝ์ ๋์ธ ์ ์์ต๋๋ค.
Clicking the button will open a new window.
๐์ ์ฐฝ์์ ํ
์คํธ ์
๋ ฅ ํ ๋ฒํผ ํด๋ฆญํ ๊ฒฝ์ฐ ์ด์ ์๋์ฐ์ ํ์๋ฉ๋๋ค.
When you enter text in a new window and click the button, it will be displayed in the previous window.
๐ ์ ์๋์ฐ์์ ์ด์ ์๋์ฐ๋ก ๊ฐ์ ์ ๋ฌ์ ์๊ทธ๋ ์ฌ๋กฏ์ ์ฌ์ฉํฉ๋๋ค.
Passing values โโfrom a new window to a previous window uses signal slots.
๐์๊ทธ๋๊ณผ ์ฌ๋กฏ์ ์ฐ๊ฒฐํ๋ฉด, ์๊ทธ๋์ด ๋ฐ์ํ ๋ ์ฌ๋กฏ์ด ์๋์ผ๋ก ์คํ๋๋ค๋ ๊ฒ๋๋ค.
When you connect a signal and a slot, the slot is automatically executed when the signal occurs.
๐์ฐฝ์ด ๊ฐ๋ ๋ค๋ฅด๋ , ๊ฐ์ฒด๊ฐ ๊ฐ๋ ๋ค๋ฅด๋ ์๊ทธ๋๊ณผ ์ฌ๋กฏ์ด ์ฐ๊ฒฐ๋์ด ์๊ณ , ์๊ทธ๋์ด ๋ฐ์ํ๋ฉด ์ฌ๋กฏ์ ๋ฌด์กฐ๊ฑด ์คํ๋ฉ๋๋ค.
Whether the windows are the same or different, whether the objects are the same or different, if a signal and a slot are connected, the slot is executed unconditionally when the signal occurs.
1.Header FIles
1)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();
// slot์ถ๊ฐ / Add slot
private slots:
void openNewWindow();
void updateLabel(const QString& text); // ์ฌ๋กฏ ์ถ๊ฐ
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
2)newwindow.h
#ifndef NEWWINDOW_H
#define NEWWINDOW_H
#include <QWidget>
class QLineEdit;
class QPushButton;
class NewWindow : public QWidget {
Q_OBJECT
public:
explicit NewWindow(QWidget *parent = nullptr);
private:
QLineEdit* inputField;
QPushButton* confirmButton;
signals:
void textSubmitted(const QString& text); // ๋ฉ์ธ์ฐฝ์ผ๋ก ๋ณด๋ผ ์๊ทธ๋
};
#endif // NEWWINDOW_H
2.Source FIles
1)main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
2)mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "newwindow.h" // ์ ์ฐฝ ํค๋ ํฌํจ
#include <QDebug> // cout <<๊ณผ ๊ฐ์ ์ญํ
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);
}
void MainWindow::openNewWindow()
{
qDebug() << "openNewWindow ์ฌ๋กฏ ์คํ๋จ/openNewWindow slot executed!";
NewWindow* window = new NewWindow(this);
window->setWindowFlags(Qt::Window); // ๋
๋ฆฝ๋ ์ฐฝ์ผ๋ก ์ค์ / Set as independent window
// ๋ถ๋ชจ ์์ด ๋
๋ฆฝ ์ฐฝ(๋๋ฒ๊น
) / Independent window without parent (debugging)
// NewWindow* window = new NewWindow(nullptr); //
// ๋ซ์ ๋ ๋ฉ๋ชจ๋ฆฌ ์๋ ํด์ / Automatically free memory when closing
window->setAttribute(Qt::WA_DeleteOnClose);
// ์๊ทธ๋๊ณผ ์ฌ๋กฏ ์ฐ๊ฒฐ / Signal and slot connection
connect(window, &NewWindow::textSubmitted, this, &MainWindow::updateLabel);
window->show();
}
void MainWindow::updateLabel(const QString& text)
{
// QLabel์ ํ
์คํธ ์ค์ / Set text to QLabel
ui->label->setText(text);
}
MainWindow::~MainWindow()
{
delete ui;
}
3)newwindow.cpp
#include "newwindow.h"
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QDebug>
NewWindow::NewWindow(QWidget *parent)
: QWidget(parent)
{
setWindowTitle("New Window");
resize(300, 200);
inputField = new QLineEdit(this);
confirmButton = new QPushButton("ํ์ธ/Confirm", this);
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addWidget(inputField);
layout->addWidget(confirmButton);
connect(confirmButton, &QPushButton::clicked, this, [this]() {
QString text = inputField->text();
// ์ฝ์ ์ถ๋ ฅ / console output
qDebug() << "์
๋ ฅ๋ ํ
์คํธ/Entered text:" << text;
// ์๊ทธ๋ ๋ฐ์ /signal generation
emit textSubmitted(inputField->text());
//close(); // ์
๋ ฅ ํ ์ฐฝ ๋ซ๊ธฐ (์ ํ์ฌํญ) / Close window after input (optional)
});
}
3.์คํ / run
