2> 完成文本编辑器的保存工作
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QFontDialog>
#include <QFont>
#include <QMessageBox>
#include <QDebug>
#include <QColorDialog>
#include <QColor>
#include <QFileDialog>
#include <QFile>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_ziti_clicked();void on_color_clicked();void on_open_clicked();void on_baocun_clicked();private:Ui::Widget *ui;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}void Widget::on_ziti_clicked()
{bool ok; //返回用户是否选中字体QFont f=QFontDialog::getFont(&ok,QFont("隶书",10,10,false),this,"选择字体");//对OK进行判断,判断用户是否选中字体了if(ok){//用户选中可以使用,该字体// ui->textEdit->setFont(f); //只能设置全部文字字体ui->textEdit->setCurrentFont(f); //设置选中的字体//ui->textEdit->setFontItacil(true); //倾斜选中的字}else{//QMessageBox::information(this,"取消","用户取消了字体");}
}void Widget::on_color_clicked()
{QColor c=QColorDialog::getColor(QColor("green"),this,"选择颜色");if(c.isValid()){//用户选择的颜色//将用户选择的颜色作用到文本上//ui->textEdit->setTextColor(c); //设置字体颜色ui->textEdit->setTextBackgroundColor(c);}else {QMessageBox::information(this,"取消","用户取消了颜色");}}void Widget::on_open_clicked()
{//QString fileName=QFileDialog::getOpenFileName(this,"选择文件","./","ALL(*.*);;Images(* . png *.xpm*.jpg);;Text file (*.txt)");if(fileName.isNull()){QMessageBox::information(this,"提示","用户取消了选择文件");return ;}qDebug()<<fileName;//文件操作//1.实例化一个对象QFile file(fileName);//2.打开文件if(!file.isOpen()){//if(!file.open(QFile::ReadWrite)){QMessageBox::critical(this,"失败","文件打开失败");return ;}}//3.读写操作QByteArray msg=file.readAll();//4.关闭文件file.close();//将读取的文本展示在ui界面ui->textEdit->setText(msg);}void Widget::on_baocun_clicked()
{QString fileName=QFileDialog::getSaveFileName(this,"选择文件","./","ALL(*.*);;Images(* . png *.xpm*.jpg);;Text file (*.txt)");if(fileName.isNull()){QMessageBox::information(this,"提示","用户取消了选择文件");return ;}qDebug()<<fileName;//实例化一个对象QFile file(fileName);if(!file.isOpen()){//if(!file.open(QFile::ReadWrite)){QMessageBox::critical(this,"失败","文件打开失败");return ;}QString msg1=ui->textEdit->toPlainText();file.write(msg1.toLocal8Bit());}else {QMessageBox::information(this,"","文件保存失败");}}
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include <QLineEdit>
#include <QDebug>
#include <QTextToSpeech> //文本转语音类
#include<QVoice>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void timerEvent(QTimerEvent *e) override;private slots:void on_pushButton_clicked();void on_pushButton_2_clicked();private:Ui::Widget *ui;int timer_id; //定时器的id号//定义一个播报员QTextToSpeech *speecher;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//给播报员实例化空间speecher = new QTextToSpeech(this);ui->QtextEdit->insertPlainText("早岁哪知世事\n仍许飞鸿荡人间\n千磨万击心铸铁\n殚精竭虑铸一剑");
}Widget::~Widget()
{delete ui;
}void Widget::on_pushButton_clicked()
{timer_id = this->startTimer(1000);//功能:启动一个定时器//参数:超时时间,每隔给定的时间后,自动调用定时器事件处理函数//返回值:当前定时器的id号
}
//定时器事件处理函数
void Widget::timerEvent(QTimerEvent *e)
{if(e->timerId() == timer_id) //说明定时器1到位{QTime sys_t = QTime::currentTime(); //获取系统时间//将QTime类对象转换为字符串QString t = sys_t.toString("hh:mm:ss");//展示到ui界面ui->QLabeltime->setText(t);if(t==ui->QLineEdit->text()){speecher->say(ui->QtextEdit->toPlainText());}}
}
void Widget::on_pushButton_2_clicked()
{this->killTimer(timer_id); //关闭给定的定时器
}
main.cpp
#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}
3.思维导图