1、完成文本编辑器的保存工作
- #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_pushButton_clicked()
- {
- bool ok; //判断是否选中字体
- QFont f= QFontDialog::getFont(&ok, QFont("宋体",10),this,"选择字体");
- //参数1:是否选中状态
- //参数2:初始字体
- //参数三:父组件
-
- if(ok)
- {
- //说明选择字体成功
- //ui->textEdit->setFont(f); 将所有字体进行改变
- ui->textEdit->setCurrentFont(f); //将选中的字体进行设置
-
- }
- else
- {
- //未选择字体
- QMessageBox::information(this,"提示","未选择字体");
- }
-
- }
- //颜色对话框
- void Widget::on_pushButton_2_clicked()
- {
- QColor c = QColorDialog::getColor(); //获取颜色
-
- //将获取的颜色放在选中的字体上
- //ui->textEdit->setTextColor(c); //设置字体颜色
- ui->textEdit->setTextBackgroundColor(c); //设置字体的背景色
- }
-
- //打开
- void Widget::on_pushButton_3_clicked()
- {
- QString fileName = QFileDialog::getOpenFileName(
- this,
- "open file",
- "./", //起始路径
- "TXT(*.txt)"
- );
- //创建文件对象,打开给定的路径下的文件
- QFile file(fileName);
- if(file.open(QFile::ReadWrite))
- {
- //打开文件,可以进行读写数据
- //将文件中的内容读取出来
- QByteArray msg = file.readAll();
-
- //将读取出来的内容放到ui界面上
- ui->textEdit->setText(QString::fromLocal8Bit(msg));
- }
- else
- {
- QMessageBox::information(this,"","文件打开失败");
-
- }
- }
-
- //另存
- void Widget::on_pushButton_4_clicked()
- {
- QString fileName = QFileDialog::getSaveFileName(
- this,
- "Save file",
- "./",
- "TXT(*.txt)");
- //创建文件对象
- QFile file(fileName);
- if(file.open(QFile::ReadWrite))
- {
- QByteArray msg =file.readAll();
- msg=ui->textEdit->toPlainText().toLocal8Bit();
- file.write(msg);
- }
- else
- {
- QMessageBox::information(this,"","文件另存失败");
- }
- }
-
2、
- #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_startBtn_clicked()
- {
- timer_id = this->startTimer(1000);
- //功能:启动一个定时器
- //参数:超时时间,每隔给定的时间后,自动调用定时器事件处理函数
- //返回值:当前定时器的id号
-
-
- }
-
-
- //关闭定时器按钮对应的槽函数
- void Widget::on_closeBtn_clicked()
- {
- this->killTimer(timer_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->timeLab->setText(t);
-
- //给播报员实例化空间
- speecher = new QTextToSpeech(this);
- if(t == ui->lineEdit->text()){
-
- speecher->say(ui->textEdit->toPlainText());
-
-
- }
-
-
- // if(e->timerId() == timer_id_1) //说明定时器1到位
- // {
-
-
- // }
- }
- }
