QT〃메모장 만들어보기

반응형


etc-image-0


글쓰기에 앞서 QT카테고리는 아직 C++과 QT의 사용법이 익숙하지 않아 여러가지를 만들어 보며 두고두고 까먹지 않도록 메모할 목적으로 만들었습니다. 


자세한 설명보다는 소스코드와 실행화면 위주이며, 이 글에서 다뤄볼 것은 디자인 UI를 사용하지 않은 메모장입니다.



실행화면


etc-image-1

먼저 메모장 기능으로 윈도우 콘솔창에 메모를 입력하는건데요.

아직 저장기능이나 버튼등 메뉴구성을 하지 않아서 미흡한 모습입니다.

조금씩 ui 기능을 넣어서 업그레이드 해보겠습니다.



 소스코드


먼저 파일을 만들어야 되는데요.

프로젝트 형식은 New Project → Qt C++ Project → Qt Gui Application으로 만들고

저는 프로젝트 이름은 test / 클래스 네임은 MainWindow로 건들지 않았습니다.


그렇다면 

Forms 폴더에 mainwindow.ui

Headers 폴더에 mainwindow.h

Source 폴더에 main.cpp / mainwindow.cpp 가 생성됩니다.


만드는 순서는 mainwindow.h 헤더파일부터~ mainwindow.cpp → main.cpp로 작업합니다.


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
// mainwindow.h
 
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
#include <QTextEdit>
#include <QtCore/QTextCodec>
 
QString changeKor(const char *);
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
 
private:
    QTextEdit *notepad;
 
    void initial();
};
 
#endif // MAINWINDOW_H
 


1) mainwindow.h 헤더파일


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
 
#include "mainwindow.h"
#include "ui_mainwindow.h"
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    initial();
}
 
MainWindow::~MainWindow()
{
 
}
 
void MainWindow::initial(){
 
    resize(400,300);    // 창의 기본크기 설정
 
    //객체 할당
    notepad = new QTextEdit(changeKor("아래에 메모하세요."));
    setCentralWidget(notepad);
}
QString changeKor(const char *strKor){
 
    static QTextCodec *codec = QTextCodec::codecForName("eucKR");
    return codec->toUnicode(strKor);
}


2) mainwindow.cpp 소스파일



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "mainwindow.h"
#include <QString>
#include <QApplication>
 
 
 
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow *window = new MainWindow;
    window->show();
    return app.exec();
}
 


3) main.cpp 소스파일



제 블로그에 포스팅되는 소스코드PC에 최적화 되어 있습니다. 모바일로 보시는 분들은 양해 부탁드립니다.


반응형