利用Qt来实现一个时钟小程序

时间:2022-10-09 00:11:37

要实现一个时钟小程序,我们首先想到需要以下的几个东西:

1. 时针

2.时针刻度

3.分针

4.分针刻度

5.秒针


要实现这个可以继承自QWidget,并在其中的paintEvent(QPaintEvent *event)Q_DECL_OVERLOAD;重载函数中来实现



具体代码如下:


#ifndef ANALOGCLOCK_H
#define ANALOGCLOCK_H

#include<QtWidgets>

class AnalogClock : public QWidget
{
    Q_OBJECT
public:
    explicit AnalogClock(QWidget *parent = 0);
    ~AnalogClock();

signals:

public slots:

protected:
    //重载函数,实现窗口的重绘
    void paintEvent(QPaintEvent* evnet)Q_DECL_OVERRIDE;
};

#endif // ANALOGCLOCK_H



#include "analogclock.h"
#include<QtWidgets>

AnalogClock::AnalogClock(QWidget *parent) : QWidget(parent)
{

    //构造函数,设置定时器
    QTimer* timer = new QTimer(this);

    connect(timer ,SIGNAL(timeout()) ,this , SLOT(update()) );

    timer->start(1000);

    setWindowTitle(tr("Analog Clock"));
    resize(200 , 200);

}

AnalogClock::~AnalogClock()
{

}


void AnalogClock::paintEvent(QPaintEvent *evnet)
{

    //时针多边形
    static const QPoint hourHand[3] = {
        QPoint(7,8),
        QPoint(-7,8),
        QPoint(0,-40)
    };
    //分针多边形
    static const QPoint minuteHand[3] = {
        QPoint(7,8),
        QPoint(-7,8),
        QPoint(0, -70)

    };

    //秒针多边形
    static const QPoint secondHand[3] = {
      QPoint(7 , 8),
        QPoint(-7 , 8),
        QPoint(0 , -90)
    };

    //设置颜色
    QColor hourColor(127 , 0 , 127);
    QColor minuteColor(0 , 127 , 127 , 191);
    QColor secondColor(0 ,0 , 127 );

    int side = qMin(width() , height());
    //获取当前时间
    QTime time = QTime::currentTime();


    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing);

    painter.translate(width()/2 , height()/2);

    painter.scale(side/200.0 , side/200.0);

    painter.setPen(Qt::NoPen);

    painter.setBrush(hourColor);

    painter.save();
    //旋转painter
    painter.rotate(30.0 * ((time.hour() + time.minute()/60.0)));
    //画指针多边形
    painter.drawConvexPolygon(hourHand , 3);

    painter.restore();

    painter.setPen(hourColor);
//画时针的刻度
    for(int i = 0 ; i < 12; ++i){
        painter.drawLine(88 , 0 , 96 , 0);
        painter.rotate(30.0);
    }


    painter.setPen(Qt::NoPen);
    painter.setBrush(minuteColor);

    painter.save();
    painter.rotate(6.0 * (time.minute() + time.second()/60));
    painter.drawConvexPolygon(minuteHand , 3);

    painter.restore();

    painter.setPen(minuteColor);

    for(int j = 0 ; j < 60 ; ++j){
        if((j %5) != 0)
        {
            painter.drawLine(92 , 0 , 96 , 0);
        }
        painter.rotate(6.0);
    }




    painter.setPen(Qt::NoPen);
    painter.setBrush(secondColor);

    painter.save();
    painter.rotate(6.0 *(time.second()));
    painter.drawConvexPolygon(secondHand , 3);
   // painter.restore();
     painter.restore();





}


然后就可以在main中使用这个时钟类了,直接实例化一个对象就可以使用了

具体如下:

 
//#include "mainwindow.h"
#include <QApplication>
#include"analogclock.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    AnalogClock w;
    w.show();

    return a.exec();
}