관리 메뉴

Value Creator의 IT(프로그래밍 / 전자제품)

#15 QFile을 이용한 메모장 만들기 본문

1. 프로그래밍/3) QT

#15 QFile을 이용한 메모장 만들기

valuecreatort 2019. 6. 19. 20:19
반응형

[과정]

 

 

 

[소스코드]

mainwindow.h

main.cpp

mainwindow.cpp

 

1) mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QAction;//
class QActionGroup;//
class QLabel;//
class QMenu;//
class QHBoxLayout;//
class QTextEdit;//

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QMenu *fileMenu;//
    QAction *openAct;//
    QHBoxLayout *layout;//
    QTextEdit *edit;//
    QString fileName;//

private slots://
    void openFile();//
};

#endif // MAINWINDOW_H

 

2) main.cpp

#include "mainwindow.h"
#include <QApplication>
#include <QStyleFactory>//

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    QStringList styles = QStyleFactory::keys();//

    a.setStyle(styles[3]);//

    w.show();

    return a.exec();
}

 

 

3) mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>//

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setFixedSize(600, 500);//

    fileMenu = menuBar() ->addMenu(tr("&File"));//

    openAct = new QAction(tr("&Open"), this);
    openAct->setShortcut(tr("Ctrl+O"));
    openAct->setStatusTip(tr("File Open"));
    connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));

    fileMenu->addAction(openAct);
    fileMenu->addSeparator();

    edit = new QTextEdit("");

    setCentralWidget(edit);

    statusBar()->showMessage(tr("File Open"));
}

MainWindow::~MainWindow()
{


    delete ui;
}

void MainWindow::openFile()
{
    fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", "ALL (*.*)");

    QFile file(fileName);
    if(file.open(QFile::ReadOnly | QFile::Text))
    {
        edit->setPlainText(file.readAll());
    }
}

 

[결과]

 

 

 

[소스코드 파일]

 

qfiletest1.zip
0.00MB

 

반응형
Comments