一、完成文本编辑器的保存工作
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_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()<<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_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 texttospeechgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += 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.0SOURCES += \main.cpp \widget.cppHEADERS += \widget.hFORMS += \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 <QWidget>
#include <QTimerEvent>
#include <QTime>
#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_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());}}
}
效果图:
三、思维导图:day2对话框补充、 有道云笔记
day3事件处理机制、有道云笔记