Qt 설치 및 간단한 사용 예
C, C++ 2019. 1. 16. 20:02 |2021.09.25 - [C, C++] - Qt6 설치 및 간단한 사용법
2021/02/13 - [C, C++] - Linux(Ubuntu) Qt5 Image Display - 리눅스(우분투) Qt5 이미지 디스플레이
2021/02/12 - [C, C++] - Linux(Ubuntu) Qt5 GUI Programming - 리눅스(우분투) Qt5 GUI 프로그래밍
Qt는 GUI 프로그램 개발에 널리 쓰이는 크로스 플랫폼 프레임워크이다. 홈페이지에서 Open Source 버전을 다운 받아 실행 한다.
기본설정에서 MSVC 2017 64-bit만 추가한다.
Qt 설치가 완료되면 Visual Studio에서도 사용 할 수 있도록 Extension을 설치 한다.
Online에서 qt를 검색해 Qt Visual Studio Tools를 설치한다.
설치가 완료되면 Qt Options를 선택 한다.
Qt가 설치된 위치의 msvc2017_64 폴더를 Add 한다.
설치가 완료 되었으면 Qt Creator를 실행하고 Qt Widgets Application 프로젝트를 만들어 준다.
프로젝트 폴더 선택 외에는 기본값으로 진행 한다.
설정이 완료되면 아래와 같은 화면이 나온다.
Forms - Mainwindow.ui 디자인 화면에서 PushButton과 Label을 적당한 위치에 배치한다. Label은 기본 크기보다 약간 크게 늘려 준다.
Label의 Property에서 alignment - Horizontal을 AlignHCenter로 바꿔준다.
PushButton에서 오른쪽 클릭 - Go to slot... 을 선택한다.
QAbstractButton - clicked() 를 선택한다.
mainWindow.cpp 에 on_pushButton_clicked() 가 생성된다.
mainwindow.h 에 QMessageBox 헤더 파일을 include 하고 IsClicked, OriginalStatus, MsgBox 멤버 변수를 추가한다.
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
 | 
 #ifndef MAINWINDOW_H 
#define MAINWINDOW_H 
#include <QMainWindow> 
#include <QMessageBox> 
namespace Ui { 
class MainWindow; 
} 
class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 
public: 
    explicit MainWindow(QWidget *parent = nullptr); 
    ~MainWindow(); 
private slots: 
    void on_pushButton_clicked(); 
private: 
    Ui::MainWindow *ui; 
    bool IsClicked; 
    QString OriginalStatus; 
    QMessageBox MsgBox; 
}; 
#endif // MAINWINDOW_H 
 | 
cs | 
mainwindow.cpp 에서 constructor와 on_pushButton_clicked() 를 아래와 같이 수정해 준다.
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
 | 
 #include "mainwindow.h" 
#include "ui_mainwindow.h" 
MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    IsClicked = false; 
    OriginalStatus = ui->pushButton->styleSheet(); 
    ui->label->setText("Unchanged"); 
    MsgBox.setWindowTitle("Sean"); 
    MsgBox.setText("Button Color Change"); 
    MsgBox.setInformativeText("Do you want to change button color?"); 
    MsgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); 
    MsgBox.setDefaultButton(QMessageBox::Yes); 
} 
MainWindow::~MainWindow() 
{ 
    delete ui; 
} 
void MainWindow::on_pushButton_clicked() 
{ 
    if (IsClicked == false) 
    { 
        int ret = MsgBox.exec(); 
        switch(ret) 
        { 
            case QMessageBox::Yes: 
                ui->pushButton->setStyleSheet("color: blue;" 
                                              "background-color: red;"); 
                ui->label->setText("Changed"); 
                IsClicked = !IsClicked; 
                break; 
            case QMessageBox::No: 
                ui->label->setText("No"); 
                break; 
            default: 
                ui->label->setText("Cancel"); 
                break; 
        } 
    } else { 
        IsClicked = !IsClicked; 
        ui->pushButton->setStyleSheet(OriginalStatus); 
        ui->label->setText("Unchanged"); 
    } 
} 
 | 
cs | 
빌드(Ctrl + B) 후 실행 (Ctrl + R) 한다.
PushButton을 클릭하면 MessageBox가 나온다.
Yes를 클릭하면 PushButton의 색과 Label 의 text가 변한다.
'C, C++' 카테고리의 다른 글
| How to make a game save file editor 간단한 게임 에디터 만들기 (1) | 2019.06.30 | 
|---|---|
| Qt 스프라이트 애니매이션 (0) | 2019.02.21 | 
| How to install and use JsonCpp - JsonCpp 설치 및 사용 (2) | 2019.01.08 | 
| Console 어플리케이션에 이미지 디스플레이 하기 (0) | 2018.11.22 | 
| Python C API (0) | 2018.11.21 |