{"id":2442,"date":"2025-10-15T14:44:13","date_gmt":"2025-10-15T05:44:13","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=2442"},"modified":"2025-10-17T11:32:24","modified_gmt":"2025-10-17T02:32:24","slug":"qtexample4-widget-new-window","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2025\/10\/15\/qtexample4-widget-new-window\/","title":{"rendered":"[QT]example7-Widget-New Window"},"content":{"rendered":"\n<p>\ud83d\udc49 \ubc84\ud2bc \ud074\ub9ad \ud560 \uacbd\uc6b0 \uc0c8 \ucc3d\uc744 \ub744\uc6b8 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>Clicking the button will open a new window.<\/p>\n\n\n\n<p>\ud83d\udc49\uc0c8 \ucc3d\uc5d0\uc11c \ud14d\uc2a4\ud2b8 \uc785\ub825 \ud6c4 \ubc84\ud2bc \ud074\ub9ad\ud560 \uacbd\uc6b0 \uc774\uc804 \uc708\ub3c4\uc6b0\uc5d0 \ud45c\uc2dc\ub429\ub2c8\ub2e4. <br>When you enter text in a new window and click the button, it will be displayed in the previous window.<\/p>\n\n\n\n<p>\ud83d\udc49 \uc0c8 \uc708\ub3c4\uc6b0\uc5d0\uc11c \uc774\uc804 \uc708\ub3c4\uc6b0\ub85c \uac12\uc758 \uc804\ub2ec\uc740 \uc2dc\uadf8\ub110 \uc2ac\ub86f\uc744 \uc0ac\uc6a9\ud569\ub2c8\ub2e4.<br>Passing values \u200b\u200bfrom a new window to a previous window uses signal slots.<\/p>\n\n\n\n<p>\ud83d\udc49\uc2dc\uadf8\ub110\uacfc \uc2ac\ub86f\uc744 \uc5f0\uacb0\ud558\uba74, \uc2dc\uadf8\ub110\uc774 \ubc1c\uc0dd\ud560 \ub54c \uc2ac\ub86f\uc774 \uc790\ub3d9\uc73c\ub85c \uc2e4\ud589\ub41c\ub2e4\ub294 \uac81\ub2c8\ub2e4.<br>When you connect a signal and a slot, the slot is automatically executed when the signal occurs.<\/p>\n\n\n\n<p>\ud83d\udc49\ucc3d\uc774 \uac19\ub4e0 \ub2e4\ub974\ub4e0, \uac1d\uccb4\uac00 \uac19\ub4e0 \ub2e4\ub974\ub4e0 \uc2dc\uadf8\ub110\uacfc \uc2ac\ub86f\uc774 \uc5f0\uacb0\ub418\uc5b4 \uc788\uace0, \uc2dc\uadf8\ub110\uc774 \ubc1c\uc0dd\ud558\uba74 \uc2ac\ub86f\uc740 \ubb34\uc870\uac74 \uc2e4\ud589\ub429\ub2c8\ub2e4.<br>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.<\/p>\n\n\n\n<p><strong>1.Header FIles<\/strong><\/p>\n\n\n\n<p>1)mainwindow.h<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#ifndef MAINWINDOW_H\n#define MAINWINDOW_H\n\n#include &lt;QMainWindow&gt;\n\nQT_BEGIN_NAMESPACE\nnamespace Ui {\nclass MainWindow;\n}\nQT_END_NAMESPACE\n\nclass MainWindow : public QMainWindow\n{\n    Q_OBJECT\n\npublic:\n    MainWindow(QWidget *parent = nullptr);\n    ~MainWindow();\n\n    \/\/ slot\ucd94\uac00 \/ Add slot\nprivate slots:\n    void openNewWindow();\n    void updateLabel(const QString&amp; text);  \/\/  \uc2ac\ub86f \ucd94\uac00\n\n\n\nprivate:\n    Ui::MainWindow *ui;\n\n\n\n};\n#endif \/\/ MAINWINDOW_H\n<\/code><\/pre>\n\n\n\n<p>2)newwindow.h<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#ifndef NEWWINDOW_H\n#define NEWWINDOW_H\n\n\n#include &lt;QWidget&gt;\n\nclass QLineEdit;\nclass QPushButton;\n\nclass NewWindow : public QWidget {\n    Q_OBJECT\n\n    public:\n        explicit NewWindow(QWidget *parent = nullptr);\n\n    private:\n        QLineEdit* inputField;\n        QPushButton* confirmButton;\n\n    signals:\n        void textSubmitted(const QString&amp; text);  \/\/ \uba54\uc778\ucc3d\uc73c\ub85c \ubcf4\ub0bc \uc2dc\uadf8\ub110\n\n};\n\n\n\n#endif \/\/ NEWWINDOW_H\n<\/code><\/pre>\n\n\n\n<p><strong>2.Source FIles<\/strong><\/p>\n\n\n\n<p>1)main.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"mainwindow.h\"\n\n#include &lt;QApplication&gt;\n\nint main(int argc, char *argv&#91;])\n{\n    QApplication a(argc, argv);\n    MainWindow w;\n    w.show();\n    return a.exec();\n}\n<\/code><\/pre>\n\n\n\n<p>2)mainwindow.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"mainwindow.h\"\n#include \".\/ui_mainwindow.h\"\n#include \"newwindow.h\"  \/\/ \uc0c8 \ucc3d \ud5e4\ub354 \ud3ec\ud568\n#include &lt;QDebug&gt; \/\/ cout &lt;&lt;\uacfc \uac19\uc740 \uc5ed\ud560\n\n\n\nMainWindow::MainWindow(QWidget *parent)\n    : QMainWindow(parent)\n    , ui(new Ui::MainWindow)\n{\n    ui-&gt;setupUi(this);\n\n    \/\/ \ubc84\ud2bc \ud074\ub9ad \uc2dc \uc0c8 \ucc3d \ub744\uc6b0\uae30\n    \/\/ Open a new window when the button is clicked\n    connect(ui-&gt;pushButton, &amp;QPushButton::clicked, this, &amp;MainWindow::openNewWindow);\n\n}\n\nvoid MainWindow::openNewWindow()\n{\n    qDebug() &lt;&lt; \"openNewWindow \uc2ac\ub86f \uc2e4\ud589\ub428\/openNewWindow slot executed!\";\n    NewWindow* window = new NewWindow(this);\n    window-&gt;setWindowFlags(Qt::Window); \/\/ \ub3c5\ub9bd\ub41c \ucc3d\uc73c\ub85c \uc124\uc815 \/ Set as independent window\n\n    \/\/ \ubd80\ubaa8 \uc5c6\uc774 \ub3c5\ub9bd \ucc3d(\ub514\ubc84\uae45) \/ Independent window without parent (debugging)\n    \/\/ NewWindow* window = new NewWindow(nullptr); \/\/\n\n    \/\/ \ub2eb\uc744 \ub54c \uba54\ubaa8\ub9ac \uc790\ub3d9 \ud574\uc81c \/ Automatically free memory when closing\n    window-&gt;setAttribute(Qt::WA_DeleteOnClose);\n\n    \/\/ \uc2dc\uadf8\ub110\uacfc \uc2ac\ub86f \uc5f0\uacb0 \/ Signal and slot connection\n    connect(window, &amp;NewWindow::textSubmitted, this, &amp;MainWindow::updateLabel);\n\n\n    window-&gt;show();\n}\n\nvoid MainWindow::updateLabel(const QString&amp; text)\n{\n    \/\/ QLabel\uc5d0 \ud14d\uc2a4\ud2b8 \uc124\uc815 \/ Set text to QLabel\n    ui-&gt;label-&gt;setText(text);\n}\n\n\nMainWindow::~MainWindow()\n{\n    delete ui;\n}<\/code><\/pre>\n\n\n\n<p>3)newwindow.cpp<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include \"newwindow.h\"\n#include &lt;QVBoxLayout&gt;\n#include &lt;QLineEdit&gt;\n#include &lt;QPushButton&gt;\n#include &lt;QDebug&gt;\n\n\nNewWindow::NewWindow(QWidget *parent)\n    : QWidget(parent)\n{\n    setWindowTitle(\"New Window\");\n    resize(300, 200);\n\n    inputField = new QLineEdit(this);\n    confirmButton = new QPushButton(\"\ud655\uc778\/Confirm\", this);\n\n    QVBoxLayout* layout = new QVBoxLayout(this);\n    layout-&gt;addWidget(inputField);\n    layout-&gt;addWidget(confirmButton);\n\n    connect(confirmButton, &amp;QPushButton::clicked, this, &#91;this]() {\n        QString text = inputField-&gt;text();\n\n        \/\/ \ucf58\uc194 \ucd9c\ub825 \/ console output\n        qDebug() &lt;&lt; \"\uc785\ub825\ub41c \ud14d\uc2a4\ud2b8\/Entered text:\" &lt;&lt; text;\n\n        \/\/ \uc2dc\uadf8\ub110 \ubc1c\uc0dd \/signal generation\n        emit textSubmitted(inputField-&gt;text());\n        \/\/close(); \/\/ \uc785\ub825 \ud6c4 \ucc3d \ub2eb\uae30 (\uc120\ud0dd\uc0ac\ud56d) \/ Close window after input (optional)\n\n    });\n}\n\n<\/code><\/pre>\n\n\n\n<p><strong>3.\uc2e4\ud589 \/ run<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"810\" height=\"645\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/10\/example7.png\" alt=\"\" class=\"wp-image-2447\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/10\/example7.png 810w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/10\/example7-300x239.png 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/10\/example7-768x612.png 768w\" sizes=\"auto, (max-width: 810px) 100vw, 810px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49 \ubc84\ud2bc \ud074\ub9ad \ud560 \uacbd\uc6b0 \uc0c8 \ucc3d\uc744 \ub744\uc6b8 \uc218 \uc788\uc2b5\ub2c8\ub2e4.Clicking the button will open a new window. \ud83d\udc49\uc0c8 \ucc3d\uc5d0\uc11c \ud14d\uc2a4\ud2b8 \uc785\ub825 \ud6c4 \ubc84\ud2bc \ud074\ub9ad\ud560 \uacbd\uc6b0 \uc774\uc804 \uc708\ub3c4\uc6b0\uc5d0 \ud45c\uc2dc\ub429\ub2c8\ub2e4. When you enter text in a new window and click the button, it will be displayed in the previous window. \ud83d\udc49 \uc0c8 \uc708\ub3c4\uc6b0\uc5d0\uc11c \uc774\uc804 \uc708\ub3c4\uc6b0\ub85c \uac12\uc758 \uc804\ub2ec\uc740 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,1],"tags":[],"class_list":["post-2442","post","type-post","status-publish","format-standard","hentry","category-qt","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=2442"}],"version-history":[{"count":10,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2442\/revisions"}],"predecessor-version":[{"id":2472,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2442\/revisions\/2472"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=2442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=2442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=2442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}