1.
头代码
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimerEvent>
#include <QTime>
#include <QTextToSpeech>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
void timerEvent(QTimerEvent *e); //定时器执行函数
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
int id; //定时器返回id
int id2;
QString s;
QTextToSpeech *speecher; //创建一个语音播报者
};
#endif // WIDGET_H
源代码
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
id = startTimer(1000); //每隔1s执行功能代码
speecher = new QTextToSpeech(this);
this->setWindowFlag(Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
}
void Widget::timerEvent(QTimerEvent *e)
{
//判断计时器id
if(e->timerId() == id)
{ //获取当前时间
QTime sys_time = QTime::currentTime();
s = sys_time.toString("hh:mm:ss");
//将获取时间放入label中
ui->sys_lab->setText(s);
//设置居中显示
ui->sys_lab->setAlignment(Qt::AlignCenter);
}else if(e->timerId() == id2)
{
if(ui->lineEdit->text() == s)
{
for(int i=0;i<5;i++)
{
speecher->say(ui->label_2->text());
}
//关闭定时器
killTimer(id2);
ui->pushButton->setText("启动");
ui->lineEdit->clear();
}
}
}
Widget::~Widget()
{
delete ui;
}
//启动按钮对应槽函数处理
void Widget::on_pushButton_clicked()
{
if(ui->pushButton->text() == "启动")
{
//启动定时器
id2 = startTimer(1000);
ui->pushButton->setText("关闭");
}else {
//关闭定时器
killTimer(id2);
ui->pushButton->setText("启动");
}
}
效果图
2.思维导图文章来源:https://www.toymoban.com/news/detail-782368.html
文章来源地址https://www.toymoban.com/news/detail-782368.html
到了这里,关于QT DAY4作业的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!