1、实现闹钟
cpp
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTime>
#include <QLineEdit>
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);
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
int id;//定时器id
};
#endif // WIDGET_H
cpp
#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、思维导图