//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:
void open();
void save();
void saveas(QString path= QString());
QString current_path;
QTextEdit* text;
QAction *openAction;
QAction *saveasAction;
QAction *saveAction;
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QPushButton"
#include "QAction"
#include "QDebug"
#include "QString"
#include "QMenuBar"
#include "QToolBar"
#include "QFileDialog"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(("Main Window"));//设置窗口的名字
openAction = new QAction(QIcon(":/image/rose"),tr("&Open..."),this);//创建一个QAction对象
openAction->setShortcut(QKeySequence::Open);//定义的该对象的快捷键
openAction->setStatusTip(tr("Open an existing file"));//实现鼠标滑过这个action在状态栏的显示
saveasAction = new QAction(QIcon(":/image/rose"),tr("&Save as..."),this);
saveasAction->setShortcut(QKeySequence::Save);
saveasAction->setStatusTip(tr("Save the file as another"));
saveAction = new QAction(QIcon(":/image/rose"),tr("Save"),this);
saveAction->setShortcut(QKeySequence::SaveAs);
saveAction->setStatusTip("save the changed file");
connect(saveAction,&QAction::triggered,this,&MainWindow::save);
connect(saveasAction,&QAction::triggered,this,[&](){
this->saveas(QFileDialog::getSaveFileName(this,"Save Files","E:\\","Text Files(*.txt)"));});//由于存在默认参数,那么函数指针不能发现该参数,所以需要用lambada表达式.
connect(openAction,&QAction::triggered,this,&MainWindow::open);
auto file = menuBar()->addMenu(tr("&File"));//创建菜单栏并且向菜单栏添加一个菜单项
file->addAction(openAction);//向这个菜单项添加action对象
file->addAction(saveasAction);
file->addAction(saveAction);
auto toolbar =addToolBar(tr("&File"));
toolbar->addAction(openAction);//向工具栏添加动作
toolbar->addAction(saveasAction);
toolbar->addAction(saveAction);
statusBar() ;//创建状态栏
text = new QTextEdit(this);//创建一个文本编辑类,用来显示富文本(图像表格等)
setCentralWidget(text);//将该控件显示在主窗的中心
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::open()
{
auto path = QFileDialog::getOpenFileName(this,"Open file","E:\\","Text Files (*.txt)");//获取文件地址
if(!path.isEmpty())//如果选中了文件
{
QFile file(path);//创建一个文件类
if(!file.open(QIODevice::ReadOnly|QIODevice::Text))//以只读形式与txt格式打开文件
{
QMessageBox::warning(this,"No files",tr("Cannot open file\n%1").arg(path));//用法见文档
return;
}
/********************成功打开文件***************/
current_path = path;//保存当前的路径
QTextStream in(&file);//从文件中读取到流
text->setText(in.readAll());//设置文本编辑框的文本,通过TextStream流的readAll()函数
file.close();//关闭文件
}
else//如果没有选中文件
QMessageBox::warning(this, tr("Path"), tr("You did not select any file."));
}
void MainWindow::saveas(QString path)
{
// path = QFileDialog::getSaveFileName(this,"Save Files","E:\\","Text Files(*.txt)");
if(!path.isEmpty())//选中了一个文件
{
QFile file(path);//创建一个文件类
if(!file.open(QIODevice::WriteOnly|QIODevice::Text))//打开不成功
{
QMessageBox::warning(this,"No Files",tr("You cannot open the files\n%1").arg(path));
return;
}
/*****打开成功******/
QTextStream out(&file);//创建一个text文件流
out<<text->toPlainText();//将文本编辑区的文字保存
file.close();
}
else
QMessageBox::warning(this,"Path","You have not selected any files");
}
void MainWindow::save()
{
saveas(current_path);
}
===========================================
//.h里面声明的
using findtype = QTextDocument::FindFlags;
private slots:
void show_findText(findtype flags);
void on_actionFind_triggered();
void MainWindow::on_actionFind_triggered()
{
auto findDlg = new QDialog(this);//创建一个用于查找的对话框
findDlg->setWindowTitle(tr("查找")); //设置对话框的标题
find_textLineEdit = new QLineEdit(findDlg);//将行编辑器加入到新建的查找对话框中
QPushButton *find_Btn_back = new QPushButton(tr("查找上一个"),findDlg);//添加查找下一个的按钮
QPushButton *find_Btn_forward = new QPushButton(tr("查找下一个"),findDlg);//添加查找下一个的按钮
auto layout = new QVBoxLayout(findDlg);//垂直布局管理器
layout->addWidget(find_textLineEdit);
layout->addWidget(find_Btn_back);
layout->addWidget(find_Btn_forward);
findDlg ->show(); //显示对话框
connect(find_Btn_back,QPushButton::clicked,[=](){show_findText(QTextDocument::FindBackward);});
connect(find_Btn_forward,QPushButton::clicked,[=](){show_findText(QTextDocument::FindFlags(0));});
}
void MainWindow::show_findText(findtype flags)
{
QString findText = find_textLineEdit->text();//获取查找内容
if(!text->find(findText,flags))
QMessageBox::warning(this,tr("查找"),tr("找不到 %1").arg(findText));
}