一、完成文本编辑器的保存工作
widget.h:
- #ifndef WIDGET_H
- #define WIDGET_H
-
-
- #include
- #include
//字体对话框 - #include
//字体类 - #include
//消息对话框 - #include
//信息调试类 - #include
//颜色对话框 - #include
//颜色类 - #include
//文件对话框类 - #include
//文件头文件 -
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class Widget; }
- QT_END_NAMESPACE
-
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
-
- public:
- Widget(QWidget *parent = nullptr);
- ~Widget();
-
-
- private slots:
- void on_fontBtn_clicked();
-
-
- void on_colorBtn_clicked();
-
-
- void on_openBtn_clicked();
-
-
- void on_saveBtn_clicked();
-
- private:
- Ui::Widget *ui;
- };
- #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);
- }
-
-
- Widget::~Widget()
- {
- delete ui;
- }
-
-
-
-
- //字体按钮对应的槽函数
- void Widget::on_fontBtn_clicked()
- {
- bool ok; //返回用户是否选中字体
-
-
- //直接调用getFont获取一个字体对话框
- 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, //父组件
- "选择颜色"); //对话框标题
-
-
- //判断c的合法性
- 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 files (*.txt);;XML files (*.xml)"); //过滤器
- //判断是否选中文件
- if(fileName.isNull())
- {
- QMessageBox::information(this,"提示","用户取消了选择文件");
- return;
- }
-
-
- qDebug()<
//得到文件路径 -
-
- //文件操作
- //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_saveBtn_clicked()
- {
- //获取保存文件对话框
- QString fileName=QFileDialog::getSaveFileName(
- this,"save file","./","All(*.*);;Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)");
-
- //判断是否选中文件
- if(fileName.isNull())
- {
- QMessageBox::information(this,"提示","用户取消了保存文件");
- return;
- }
-
- //文件操作
- //1、实例化一个文件对象
- QFile file(fileName);
-
-
- //2、打开文件
- if(!file.isOpen()) //如果该文件没有被打开,则打开文件
- {
- //调用打开文件操作
- if(!file.open(QFile::ReadWrite))
- {
- QMessageBox::critical(this, "失败","文件打开失败");
- return; //文件打开失败
- }
- }
-
- //3、读写操作
- QString msg = ui->textEdit->toPlainText();
- file.write(msg.toUtf8());
-
- //4、关闭文件
- file.close();
- }
成品效果图:

二、

hwk3.pro:
- QT += core gui texttospeech
-
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
-
- CONFIG += c++11
-
- # The following define makes your compiler emit warnings if you use
- # any Qt feature that has been marked deprecated (the exact warnings
- # depend on your compiler). Please consult the documentation of the
- # deprecated API in order to know how to port your code away from it.
- DEFINES += QT_DEPRECATED_WARNINGS
-
- # You can also make your code fail to compile if it uses deprecated APIs.
- # In order to do so, uncomment the following line.
- # You can also select to disable deprecated APIs only up to a certain version of Qt.
- #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
-
- SOURCES += \
- main.cpp \
- widget.cpp
-
- HEADERS += \
- widget.h
-
- FORMS += \
- widget.ui
-
- # Default rules for deployment.
- qnx: target.path = /tmp/$${TARGET}/bin
- else: unix:!android: target.path = /opt/$${TARGET}/bin
- !isEmpty(target.path): INSTALLS += target
widget.h:
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
- #include
- #include
- #include
//文本转语音类 - #include
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class Widget; }
- QT_END_NAMESPACE
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- public:
- Widget(QWidget *parent = nullptr);
- ~Widget();
-
- void timerEvent(QTimerEvent *e) override; //闹钟事件处理函数
-
- private slots:
- void on_Btn1_clicked();
-
- void on_Btn2_clicked();
-
- private:
- Ui::Widget *ui;
-
- //定义一个播报员
- QTextToSpeech *speecher;
-
- int timer_id; //定时器id号
- int timer_id1; //系统定时器id号
- };
- #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);
- }
-
- Widget::~Widget()
- {
- delete ui;
-
- }
-
- //启动定时器按钮对应的槽函数
- void Widget::on_Btn1_clicked()
- {
- timer_id = this->startTimer(1000);
- timer_id1 = this->startTimer(1000);
- }
-
- //启动定时器按钮对应的槽函数
- void Widget::on_Btn2_clicked()
- {
- this->killTimer(timer_id1);
- }
-
- void Widget::timerEvent(QTimerEvent *e)
- {
- if(e->timerId() == timer_id)
- {
- QTime sys_t = QTime::currentTime(); //获得系统时间
- //将QTime类对象转换为字符串
- QString t1 = sys_t.toString("hh:mm:ss");
-
- //展示到ui界面
- ui->timeLab->setText(t1);
- }
- if(e->timerId() == timer_id1)
- {
- QTime sys_t1 = QTime::currentTime(); //获得系统时间
- //将QTime类对象转换为字符串
- QString t1 = sys_t1.toString("hh:mm:ss");
-
- if(t1==ui->lineEdit->text())
- {
- speecher->say(ui->textEdit->toPlainText());
- }
- }
- }
效果图:

day3事件处理机制、有道云笔记