文件
代码
clock.h
#include "clock.h"
#include "ui_clock.h"
Clock::Clock(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Clock)
, ptrTimerShow(new QTimer(this))
, ptrTimerAlarm(new QTimer(this))
, ptrSpeecher(new QTextToSpeech)
{
ui->setupUi(this);
connect(ptrTimerShow, &QTimer::timeout, this, &Clock::timeShowSlot);
connect(ptrTimerAlarm, &QTimer::timeout, this, &Clock::timeAlarmSlot);
ptrTimerShow->start(1000);
}
Clock::~Clock()
{
delete ui;
}
void Clock::on_btnStart_clicked()
{
QString txtTime = ui->lineEditTime->text();
if (! txtTime.isEmpty())
{
ui->lblInput->setText(txtTime);
QTime alarmTime = QTime::fromString(txtTime);
int msec = alarmTime.msecsSinceStartOfDay() - QTime::currentTime().msecsSinceStartOfDay();
qDebug("msec = %d", msec);
ptrTimerAlarm->start(msec);
}
}
void Clock::timeShowSlot()
{
QTime sysTime = QTime::currentTime();
QString strSysTime = sysTime.toString("hh:mm:ss");
ui->lblSysTime->setText(strSysTime);
ui->lblSysTime->setAlignment(Qt::AlignCenter);
}
void Clock::timeAlarmSlot()
{
qDebug("time out\n");
ptrTimerAlarm->stop();
QString info = "wake up, good good study, day day up";
ui->lblInfo->setText(info);
for(int i=0; i<5; i++)
{
ptrSpeecher->say(info);
}
}
clock.cpp?
#ifndef CLOCK_H
#define CLOCK_H
#include <QWidget>
#include <QTimer>
#include <QTime>
#include <QDateTime>
#include<QDebug>
#include<QTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class Clock; }
QT_END_NAMESPACE
class Clock : public QWidget
{
Q_OBJECT
public:
Clock(QWidget *parent = nullptr);
~Clock();
private slots:
void on_btnStart_clicked();
private:
Ui::Clock *ui;
public:
QTimer *ptrTimerShow;
QTimer *ptrTimerAlarm;
public slots:
void timeShowSlot();
void timeAlarmSlot();
private:
QTextToSpeech *ptrSpeecher;
};
#endif // CLOCK_H
man.cpp
#include "clock.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Clock w;
w.show();
return a.exec();
}
运行界面