1. 思维导图
2. 编写程序实现闹钟如下
文件
代码
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();
}
运行界面文章来源:https://www.toymoban.com/news/detail-790298.html
文章来源地址https://www.toymoban.com/news/detail-790298.html
到了这里,关于Qt / day04的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!