1> 完成文本编辑器的保存工作
ui界面:
头文件:
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QFontDialog>
#include <QMessageBox>
#include <QColor>
#include <QColorDialog>
#include <QFont>
#include <QDebug>
#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
cpp文件:
main:
#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}
功能函数:
#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;QFont f=QFontDialog::getFont(&ok,QFont("隶书",10,10,false),this,"选择字体");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 files(*.txt);;XML files(*.xml)");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->textEdit->setText(msg);//将内容展示在ui界面}void Widget::on_saveBtn_clicked()
{QString fileName=QFileDialog::getSaveFileName(this,"保存文件","./","All(*.*);;Images(*.png *.xpm *.jpg);;Text files(*.txt);;XML files(*.xml)");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 msg=ui->textEdit->toPlainText();//读写操作file.write(msg.toUtf8());//将ui界面内容编码格式改为utf-8,并写入保存文件中file.close();//关闭文件}
2>完成如下闹钟功能:
当前时间为本地时间,编辑的时间自定义,启动后当时间相等时,自动播报文本编辑内容,按下停止按钮结束播报
ui界面:
pro文件 :
cpp:
main:
#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}
功能函数:
#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);speecher = new QTextToSpeech(this);ui->textEdit->insertPlainText("三更灯火五更鸡\n正是男儿读书时\n黑发不知勤学早\n白首方悔读书迟");}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:ss");ui->label->setText(t);QString b=ui->lineEdit->text();if(t == b){speecher->say(ui->textEdit->toPlainText());}}
}void Widget::on_pushButton_clicked()
{}void Widget::on_pushButton_2_clicked()
{speecher->stop();ui->lineEdit->clear();
}
头文件:
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include <QTextToSpeech> //文本转语音类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;//定义一个播报员QTextToSpeech *speecher;
};
#endif // WIDGET_H