Qt / day04

发布时间:2024年01月11日

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://blog.csdn.net/K_A_Wang/article/details/135538273
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。