#include "clockwindow.h"
#include "ui_clockwindow.h"
ClockWindow::ClockWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::ClockWindow)
, time(new QTimer(this))
, speech(new QTextToSpeech(this))
{
ui->setupUi(this);
connect(time, &QTimer::timeout, this, &ClockWindow::my_slot);
time->start(1000);
}
ClockWindow::~ClockWindow()
{
delete ui;
}
void ClockWindow::on_startBtn_clicked()
{
if(ui->startBtn->text() == "启动")
{
//启动一个定时器
it = startTimer(1000); //启动一个定时器,每隔1秒钟,自动执行timerEvent()
//将按钮设置成关闭
ui->startBtn->setText("关闭");
}
// else
// {
// }
}
void ClockWindow::timerEvent(QTimerEvent *){
QTime t = QTime::currentTime();
QString s = t.toString();
QString settime = ui->textEdit->toPlainText();
if(settime == s /*|| settime < s*/){
for(int i = 0; i < 2; i++){
speech->say(ui->voice->text());
}
//关闭定时器
killTimer(it);
//将按钮设置启动
ui->startBtn->setText("启动");
}
}
void ClockWindow::my_slot()
{
//设置系统时间
QTime t = QTime::currentTime();
QString s = t.toString();
ui->label->setText(QString("系统时间:%1").arg(s));
}
闹钟20240618
于 2024-06-18 22:32:17 首次发布