您的位置:首页 > 移动开发 > 微信开发

Qt实现记事本的打开和保存功能(第一个实用的Qt小程序)

2016-03-30 23:16 696 查看
学习qt也有两三天时间,Qt的学习资料比较少,相比C++而言, Qt的资料是比较小的,Qt5就更少了。

这里我首先要推荐一个博客:www.devbean.net/2012/08/qt-study-road-2-catelog/对我学习Qt5很有帮助

首先把主要的代码贴出来然后再解释一下吧:

// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTextEdit>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

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

private:
Ui::MainWindow *ui;
void openFile();
void saveFile();

QTextEdit *textEdit;
QAction *openAction;
QAction *saveAction;
};

#endif // MAINWINDOW_H


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextEdit>
#include <QFile>
#include <QTextStream>
#include <QFileDialog>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// ui->setupUi(this);
this->resize(800, 600);

openAction = new QAction(QIcon(":/images/open"), tr("&Open"), this);
openAction->setShortcut(QKeySequence::Open);
openAction->setStatusTip(tr("Open an exisiting file"));

saveAction = new QAction(QIcon(":/images/save"), tr("&Save"), this);
saveAction->setShortcut(QKeySequence::Save);
saveAction->setStatusTip(tr("Save a file"));

connect(openAction, &QAction::triggered, this, &MainWindow::openFile);
connect(saveAction, &QAction::triggered, this, &MainWindow::saveFile);

QMenu *file = menuBar()->addMenu(tr("&File"));
file->addAction(openAction);
file->addAction(saveAction);

QToolBar *toolBar = addToolBar(tr("&File"));
toolBar->addAction(openAction);
toolBar->addAction(saveAction);

textEdit = new QTextEdit(this);
setCentralWidget(textEdit);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::openFile()
{
QString path = QFileDialog::getOpenFileName(this,
tr("Open file"),
"/",
tr("*.txt"));
if(!path.isEmpty()){
QFile file(path);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
QMessageBox::warning(this, tr("Read File"),
tr("Can't open file:\n%1").arg(path));
return;
}
QTextStream in(&file);
textEdit->setText(in.readAll());
file.close();
} else {
QMessageBox::warning(this, tr("Path"),
tr("You didn't select any file."));
}
}

void MainWindow::saveFile()
{
QString path = QFileDialog::getSaveFileName(this,
tr("Open File"),
".",
tr("Text Files(*.txt)"));
if(!path.isEmpty()){
QFile file(path);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
QMessageBox::warning(this, tr("Write File"),
tr("Can't open file:\n%1").arg(path));
return;
}
QTextStream out(&file);
out << textEdit->toPlainText();
file.close();
} else {
QMessageBox::warning(this, tr("Path"),
tr("You did not select any file."));
}
}
主要是这个两个文件,第一个头文件也就不多解释了,主要是定义了两个动作。

然后在第二个文件中的构造函数中我添加了菜单栏和工具栏,并添加了两个动作:打开和保存。还有在主窗口的中间添加了一个QTextEide文本,这个类是一个富文本,不仅仅可以添加文本,还可以添加图片,图表等,我们这里只是采用文本。

接下来是两个函数,自定义信号槽。

第一个openFile函数显而易见就是打开文件用的。

在openFile函数中我们用了OFileDialog::getOpenFileName函数,这个函数的函数原型比较长:

QString getOpenFileName(QWidget * parent = 0,
const QString & caption = QString(),
const QString & dir = QString(),
const QString & filter = QString(),
QString * selectedFilter = 0,
Options options = 0)


但是很多参数都是带有默认值的,六个参数的函数分别是:

parent:父窗口。我们前面介绍过,Qt 的标准对话框提供静态函数,用于返回一个模态对话框(在一定程度上这就是外观模式的一种体现);
caption:对话框标题;
dir:对话框打开时的默认目录,“.” 代表程序运行目录,“/” 代表当前盘符的根目录(特指 Windows 平台;Linux 平台当然就是根目录),这个参数也可以是平台相关的,比如“C:\\”等;
filter:过滤器。我们使用文件对话框可以浏览很多类型的文件,但是,很多时候我们仅希望打开特定类型的文件。比如,文本编辑器希望打开文本文件,图片浏览器希望打开图片文件。过滤器就是用于过滤特定的后缀名。如果我们使用“Image Files(*.jpg *.png)”,则只能显示后缀名是 jpg 或者 png 的文件。如果需要多个过滤器,使用“;;”分割,比如“JPEG Files(*.jpg);;PNG Files(*.png)”;
selectedFilter:默认选择的过滤器;
options:对话框的一些参数设定,比如只显示文件夹等等,它的取值是
enum QFileDialog::Option
,每个选项可以使用 | 运算组合起来。

首先我们穿件一个File对象,然后使用QFileDialog打开一个文本,打开成功就用QFileStream读取所有内容并显示在主窗口的QTextEdie组件中,然后把文件关闭。

这是运行效果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: