C++-Qt【2】-实现一个简单的记事本

用Qt实现一个简单的记事本:

#include "helloqt.h"
#include <qfiledialog.h>
#include <qfile.h>
#include <qmessagebox.h>
#include <qtextstream.h>
#include <qdebug.h>

//#if _MSC_VER >= 1600      
//#pragma execution_character_set("utf-8")      
//#endif

HelloQt::HelloQt(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
}

HelloQt::~HelloQt()
{

}

void HelloQt::on_pushButton_clicked() {
    qDebug() << "new line";
    ui.textEdit->append("你点击了我!");
}

void HelloQt::on_action_Open_triggered() {
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(),
        tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));

    if (!fileName.isEmpty()) {
        QFile file(fileName);
        if (!file.open(QIODevice::ReadOnly)) {
            QMessageBox::critical(this, tr("Error"), tr("Could not open file"));
            return;
        }
        QTextStream in(&file);
        ui.textEdit->setText(in.readAll());
        file.close();
    }
}

void HelloQt::on_action_Save_triggered() {
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QString(),
        tr("Text Files (*.txt);;C++ Files (*.cpp *.h)"));

    if (!fileName.isEmpty()) {
        QFile file(fileName);
        if (!file.open(QIODevice::WriteOnly)) {
            // error message
        }
        else {
            QTextStream stream(&file);
            stream << ui.textEdit->toPlainText();
            stream.flush();
            file.close();
        }
    }
}

void HelloQt::on_action_Exit_triggered() {
    qApp->exit(0);
}

 

posted @ 2016-09-29 14:22  上帝之城  阅读(1358)  评论(0编辑  收藏  举报