[QT]example7-Widget-New Window

๐Ÿ‘‰ ๋ฒ„ํŠผ ํด๋ฆญ ํ•  ๊ฒฝ์šฐ ์ƒˆ ์ฐฝ์„ ๋„์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
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

Leave a Reply

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