2> 完成文本编辑器的保存工作
widget.cpp
#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_fontbtn_clicked()
{bool ok;//调用grtfont获取一个字体对话框QFont f=QFontDialog::getFont(&ok,QFont("隶书",10,10,false),this,"选择字体");//对OK进行判断,判断用户是否选择字体if(ok){//用户选择字体//将选中的字体,进行设置//设置全部文字字体//ui->textEdit->setFont(f);//设置选中的字体ui->textEdit->setCurrentFont(f);//设置选中字体倾斜//ui->textEdit->setFontItalic(true);}else{//用户没有选择字体QMessageBox::information(this,"取消","用户取消选择字体");}
}
//颜色按钮对应的槽函数
void Widget::on_colorbtn_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_openbtn_clicked()
{QString fileName = QFileDialog::getOpenFileName(this,"选择文件","./","ALL(*.*);;Images(*.png *.xpm *.jpg)::Text file (*.text)");//判断是否选择文件if(fileName.isNull()){QMessageBox::information(this,"提示","用户取消了选择");return;}qDebug()<<fileName;//实例化文件对象QFile file(fileName);//打开文件if(!file.isOpen()){//调用打开文件操作if(!file.open(QFile::ReadWrite)){QMessageBox::critical(this,"失败","文件打开失败");return;}}//读文件QByteArray msg =file.readAll();//关闭文件file.close();//将读取的文件展示在UI界面ui->textEdit->setText(msg);}
//保存
void Widget::on_savebtn_clicked()
{QString fileName = QFileDialog::getSaveFileName(this,"保存文件","./","ALL(*.*);;Images(*.png *.xpm *.jpg)::Text file (*.text)");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 text =ui->textEdit->toPlainText();file.write(text.toUtf8());file.close();}
3、定时器播报
wiget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);timer_id = this->startTimer(1000);tt=new QTextToSpeech(this);
}Widget::~Widget()
{delete ui;
}void Widget::timerEvent (QTimerEvent *e)
{if(e->timerId()==timer_id){QTime sys_t =QTime::currentTime();QString t=sys_t.toString("hh.mm");ui->label->setText(t);}else if(e->timerId()==timer_id2){QString t=ui->lineEdit->text();QString t1=ui->label->text();if(t==t1){QString a1=ui->textEdit->toPlainText();tt->say(a1);}}}
void Widget::on_pushButton_clicked()
{timer_id2 = this->startTimer(3000);
}void Widget::on_pushButton_2_clicked()
{tt->stop();this->killTimer(timer_id2);
}
wiget.h