1.디자인만들기 / Create a design
— mainwindow.ui를 더블클릭하면 디자인을 만들 수 있습니다.
You can create a design by double-clicking mainwindow.ui.

— 좌측의 Widget Box를 이용해서 다음과 같이 화면을 구성합니다.
— objectName은 우측의 패널에서 바꿀 수 있습니다.
1)레이아웃:Vertical Layout
2) 버튼 : Push Button
==> objectName : pushButton
3)입력 : PlainText Edit
==> 입력1 : objectName : plainTextEdit
==> 입력2 : objectName : plainTextEdit
4)값 출력 : Label
==> objectName : label

main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
// 프로그램 시작점 최소한의 코딩
// Program starting point with minimal coding
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
// mainWindow는 UI + 연결 역할 담당
// mainWindow is responsible for UI + connection.
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
// ui초기화[기본코드](Qt Designer에서 만든 UI 불러오기)
// UI initialization [basic code] (loading UI created in Qt Designer)
ui->setupUi(this);
//
// 생성자 안에서 한 번만 설정,탭키 누르면 다음 텍스트로 이동
// Set once in constructor, press tab key to move to next text
//
// 여기서부터 코딩시작 / Start coding from here -->
//
ui->plainTextEdit->setTabChangesFocus(true);
ui->plainTextEdit2->setTabChangesFocus(true);
// 버튼 클릭 시 입력값을 라벨에 출력
//
When a button is clicked, the input value is output to the label.3
connect(ui->pushButton, &QPushButton::clicked, this, [=](){
// 두 개의 QPlainTextEdit에서 텍스트 가져오기
// Get text from two QPlainTextEdits
QString text1 = ui->plainTextEdit->toPlainText();
QString text2 = ui->plainTextEdit2->toPlainText();
// 문자열 연결
// string concatenation
QString combined = text1 + text2;
// label에 출력
// Print on label
ui->label->setText(combined);
});
//<-- 여기까지 코딩 / Coding up to here
}
MainWindow::~MainWindow()
{
delete ui;
}
1.코드 설명 / Code Description
— 텍스트 입력 2개에서 받은 값을 버튼 누를 경우 라벨에 출력하는 예제 입니다.
This is an example of printing the values received from two text inputs to a label when a button is pressed.
1).시그널과 슬롯 / SIgnal and Slots
— 굵은 글자 부분이 시그널과 슬롯을 사용하는 구조입니다.
The bold part is the structure that uses signals and slots.
–– &QPushButton::clicked
==> 이 부분은 시그널, 이벤트 발생하는 부분 입니다.
&QPushButton::clicked ==> This part is where the signal and event occur.
— [=](){} ==> 이 Lambda부분이 실행될 함수가 호출 되는 부분 입니다.
[=](){} ==> This Lambda part is where the function to be executed is called.
connect(ui->pushButton, &QPushButton::clicked, this, [=](){
// 두 개의 QPlainTextEdit에서 텍스트 가져오기
QString text1 = ui->plainTextEdit->toPlainText();
QString text2 = ui->plainTextEdit2->toPlainText();
// 문자열 연결
QString combined = text1 + text2;
// label에 출력
ui->label->setText(combined);
});
Qt | C++ 구조 |
---|---|
connect(button, &QPushButton::clicked, this, [](){ ... }); | 버튼 클릭 → 멤버 함수 포인터 & Lambda 호출 Click button → Call member function pointer & Lambda |
clicked → 시그널/Signal | 멤버 함수 포인터 member function pointer |
Lambda → 슬롯/Slot | C++ 함수 호출 C++ function call |