10.20作业

闹钟

10.20作业_第1张图片 

main.cpp
#include "widget.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}
 widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 

QT_BEGIN_NAMESPACE
namespace Ui
{
    class Widget;
}
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget* parent = nullptr);
    ~Widget();
    void timerEvent(QTimerEvent* event) override;
private slots:
    void on_set_clock_clicked();

    void on_cancel_clock_clicked();

private:
    Ui::Widget* ui;
    QTime clock_time;
    int tid;
    int tid2;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget* parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    tid = startTimer(1000);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::timerEvent(QTimerEvent* event)
{
    QTime t = QTime::currentTime();

    if(tid == event->timerId()) {
        ui->lcd_time->display(t.toString());
    }

    if(tid2 == event->timerId()
            && t.hour() == clock_time.hour()
            && t.minute() == clock_time.minute()) {
        ui->lineEdit->setText("滴滴滴滴滴");
    }

}

void Widget::on_set_clock_clicked()
{
    clock_time = ui->timeEdit->time();
    tid2 = startTimer(1000);
    ui->lineEdit->setText("闹钟开启中...");
}

void Widget::on_cancel_clock_clicked()
{

    killTimer(tid2);
    ui->lineEdit->setText("闹钟未开启");
}

你可能感兴趣的:(qt,c++,开发语言)