下面是效果图:
附上核心代码:
1.widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
protected:
void paintEvent(QPaintEvent *event);//添加重绘事件
};
#endif // WIDGET_H
2.widget.cpp:
#include "widget.h"
#include "ui_widget.h"
#include<QPainter>
#include<QtGui>
#include<QDebug>
#include<QFont>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
QTimer *timer = new QTimer(this);//创建一秒定时器
connect(timer, SIGNAL(timeout()), this, SLOT(update()));//信号与槽关联
timer->start(1000);
setWindowTitle(tr("时钟"));
setAutoFillBackground(true