Qt实现一个简单的钟表

效果图

Qt实现一个简单的钟表_第1张图片

实现思路

1.先插入一张自己喜欢的图片当背景。
2.调用绘图函数画出时针,分针,秒针。
3.调用绘图函数画出表盘。(如果图片有表盘也可以不画)
4.用QTimer设定时间,使钟表一秒钟刷新一次。

代码分享

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include 
#include

QT_BEGIN_NAMESPACE
namespace Ui { class Dialog; }
QT_END_NAMESPACE

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();
    void paintEvent(QPaintEvent* event);
    static const QPoint hourHand[4];
    static const QPoint minuteHand[4];
    static const QPoint secondHand[4];
    QPen hourHandPen;
    QPen minuteHandPen;
    void drawHourHand(QPainter *painter);
    void drawMinuteHand(QPainter *painter);
    void drawsecondHand(QPainter *painter);
    void drawClockDial(QPainter *painter);

private:
    Ui::Dialog *ui;
};
#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include
#include
#include
#include
const QPoint Dialog::hourHand[4] = {
    QPoint(2, 10),
    QPoint(-2, 10),
    QPoint(-2, -40),
    QPoint(2, -40)
};
const QPoint Dialog::minuteHand[4] = {
    QPoint(2, 15),
    QPoint(-2, 15),
    QPoint(-2, -70),
    QPoint(2, -70)
};
const QPoint Dialog::secondHand[4] = {
    QPoint(1, 18),
    QPoint(-1, 18),
    QPoint(-1, -90),
    QPoint(1, -90)
};

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    hourHandPen = QPen(palette().foreground(), 2.0);//设置小时刻度线为粗黑
    minuteHandPen = QPen(palette().foreground(), 1.0);//设置分钟刻度线为灰
    QTimer *timer = new QTimer(this);
    timer->start(1000);//一秒钟
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));
    setWindowTitle("The Clock");
    resize(500,500);
}

Dialog::~Dialog()
{
    delete ui;
}
void Dialog::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QRect r1(0,0,500,500);
    QPixmap m(":/res/2_4.jpg");
    painter.drawPixmap(r1,m,r1);
    painter.translate(250,250);
    drawHourHand(&painter);
    drawMinuteHand(&painter);
    drawsecondHand(&painter);
    drawClockDial(&painter);
    painter.setBrush(Qt::black);
    painter.drawEllipse(QPoint(0,0),3,3);


}
void Dialog::drawHourHand(QPainter *painter)
{
    QTime time = QTime::currentTime();
    painter->setBrush(Qt::black);
    painter->setPen(Qt::black);
    painter->save();
    painter->rotate(30.0*(time.hour()+time.minute()/60.0));
    painter->drawConvexPolygon(hourHand,4);//绘制时针
    painter->restore();//绘制图形后复位坐标系
}
void Dialog::drawMinuteHand(QPainter *painter)
{
    QTime time = QTime::currentTime();
    painter->setBrush(Qt::black);
    painter->setPen(Qt::black);
    painter->save();
    painter->rotate(6.0*(time.minute()+time.second()/60.0));
    painter->drawConvexPolygon(minuteHand,4);//绘制分针
    painter->restore();//绘制图形后复位坐标系
}
void Dialog::drawsecondHand(QPainter *painter)
{
    QTime time = QTime::currentTime();
    painter->setBrush(Qt::red);
    painter->setPen(Qt::red);
    painter->save();//保存坐标系,防止坐标系跑偏了
    painter->rotate(6.0*time.second());//注意是6.0,不是6
    painter->drawConvexPolygon(secondHand,4);//绘制秒针
    painter->restore();//绘制图形后复位坐标系
}
void Dialog::drawClockDial(QPainter *painter)
{
    //绘制钟表刻度盘和数字
    for (int i = 1; i <=60; ++i)
    {
        painter->save();
        painter->rotate(6*i);//坐标轴旋转6度
        if (i % 5 == 0)
        {
            painter->setPen(hourHandPen);
            painter->drawLine(0, -190, 0, -170);
            painter->drawText(-20, -170, 40, 40,
                              Qt::AlignHCenter | Qt::AlignTop,
                              QString::number(i/5));
        }
        else
        {
            painter->setPen(minuteHandPen);
            painter->drawLine(0, -180, 0, -170);
        }
        painter->restore();//绘制图形后复位坐标系
    }
}


你可能感兴趣的:(Qt实现一个简单的钟表)