1、实现闹钟
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QTime>
#include <QLineEdit>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void timerEvent(QTimerEvent *event);private slots:void on_pushButton_clicked();private:Ui::Widget *ui;int id;//定时器id
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);id = startTimer(1000);
}Widget::~Widget()
{delete ui;
}void Widget::timerEvent(QTimerEvent *event)
{//获取系统时间QTime sys_time = QTime::currentTime();//转换为字符串QString s = sys_time.toString("hh:mm:ss");//将时间设置到ui界面的lab中ui->sys_time->setText(s);//比较时间if(ui->sys_time->text() == ui->clock_lab->text()){ui->show_lab->setText("R U OK???");}//居中ui->sys_time->setAlignment(Qt::AlignCenter);
}void Widget::on_pushButton_clicked()
{QString s= ui->lineEdit->text();ui->clock_lab->setText(s);
}
2、思维导图