#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
obj_timer = new QTimer(this);
QPixmap background("D:E:\\QT\\qt\\photo\\b53cc669a88724ab0a47a3eb149d75e.png"); // 替换为你的背景图路径
QPalette palette;
palette.setBrush(this->backgroundRole(), QBrush(background));
this->setPalette(palette);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_eventBtn_clicked()
{
ui->eventBtn->setEnabled(false);
ui->eventBtn_02->setEnabled(true);
ui->lineEdit->setEnabled(false);
if(ui->eventBtn->text() == "启动")
{
//启动一个定时器
event_timer = this->startTimer(1000);
//此时启动了有一个定时器,
}else
{
//关闭一个定时器
this->killTimer(event_timer);
}
}
//定时器事件处理函数的定义
void Widget::timerEvent(QTimerEvent *e)
{
//判断是哪个定时器到位
if(e->timerId() == event_timer)
{
//获取系统日期时间
QDateTime sys_dt = QDateTime::currentDateTime();
//将该时间转换为字符串并展示到ui界面上
ui->label->setText(sys_dt.toString("yyyy-MM-dd hh:mm:ss"));
time = sys_dt.toString("yyyy-MM-dd hh:mm:ss");
qDebug()<<time;
QString settime=ui->lineEdit->text();
if(settime==time)
{
speecher=new QTextToSpeech(this);
QString msg=ui->textEdit->toPlainText();
//语音播报文本内容
speecher->say(msg);
qDebug()<<msg;
}
}
}
void Widget::on_eventBtn_02_clicked()
{
ui->eventBtn->setEnabled(true);
ui->eventBtn_02->setEnabled(false);
ui->lineEdit->setEnabled(true);
if(ui->eventBtn_02->text() == "关闭")
{
this->killTimer(event_timer);
}
}
头
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QWidget>
#include <QTimer>
#include <QTime>
#include <QTimerEvent>
#include <QDateTime>
#include <QtTextToSpeech>
#include <QCoreApplication>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
void timerEvent(QTimerEvent *e);
~Widget();
private slots:
// void timeout_slot();
void on_eventBtn_clicked();
void on_eventBtn_02_clicked();
private:
Ui::Widget *ui;
QTimer *obj_timer;
int event_timer;
QString time;
QTextToSpeech *speecher;
};
#endif // WIDGET_H

该代码示例展示了如何在Qt环境中创建一个带有图形界面的应用,该应用使用QTimer事件来更新时间显示,并在特定条件下通过QTextToSpeech进行语音播报。用户可以启动和关闭定时器,以及触发语音播报功能。

6203

被折叠的 条评论
为什么被折叠?



