欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Qt学习笔记-自绘时钟

程序员文章站 2022-05-28 21:52:17
...

运行截图如下(时针,分针,秒针都可以动的哦):

Qt学习笔记-自绘时钟

代码如下:

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();

protected:
    void paintEvent(QPaintEvent *)override;
    void resizeEvent(QResizeEvent *event)override;

    void mouseMoveEvent(QMouseEvent *event)override;
    void mousePressEvent(QMouseEvent *event)override;
    void mouseReleaseEvent(QMouseEvent *event)override;
private:
    Ui::Widget *ui;
    bool m_dragging;
    QPoint m_startPosition;
    QPoint m_framePosition;
};

#endif // WIDGET_H

Widget.cpp

#include "widget.h"
#include "ui_widget.h"

#include <QPainter>
#include <QTime>
#include <QMouseEvent>
#include <QTImer>

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);
    m_dragging = false;
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *){
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.translate(width()/2,height()/2);
    int side=qMin(width(),height());
    painter.scale(side/200.0,side/200.0);

    QPen pen(Qt::black,4);
    painter.setPen(pen);
    QBrush brush(QColor(247,238,214,80));
    painter.setBrush(brush);
    painter.drawEllipse(QPointF(0,0),98,98);

    painter.setPen(Qt::white);
    painter.drawEllipse(QPointF(0,0),100,100);

    QTime time=QTime::currentTime();
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::black);

    painter.save();
    painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));

    static const QPoint hourHand[3] = {
        QPoint(2, 0),
        QPoint(-2, 0),
        QPoint(0, -60)
    };
    painter.drawConvexPolygon(hourHand, 3);
    painter.restore();

    static const QPoint minuteHand[3] = {
        QPoint(2, 0),
        QPoint(-2, 0),
        QPoint(0, -95)
    };

    static const QPoint secondHand[3]={
        QPoint(2,0),
        QPoint(-2,0),
        QPoint(0,-105)
    };

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

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



    painter.save();
    painter.setPen(Qt::black);
    painter.setBrush(Qt::lightGray);
    painter.drawEllipse(QPointF(0, 0), 2, 2);
    painter.drawPoint(0, 0);
    painter.restore();

    painter.setPen(Qt::black);
    for (int i = 0; i < 12; ++i) {
        painter.drawLine(90, 0, 96, 0);
        painter.rotate(30.0);
    }
    for (int j = 0; j < 60; ++j) {
        if ((j % 5) != 0)
            painter.drawLine(95, 0, 96, 0);
        painter.rotate(6.0);
    }
}

void Widget::mousePressEvent(QMouseEvent *event){
    if(event->button()==Qt::LeftButton){
        m_dragging=true;
        m_startPosition=event->globalPos();
        m_framePosition=frameGeometry().topLeft();
    }
    QWidget::mousePressEvent(event);
}

void Widget::mouseMoveEvent(QMouseEvent *event){
    if(event->buttons()&Qt::LeftButton){
        if(m_dragging){
            QPoint delta=event->globalPos()-m_startPosition;
            move(m_framePosition+delta);
        }
    }
    QWidget::mouseMoveEvent(event);
}

void Widget::resizeEvent(QResizeEvent *event){
    int side = qMin(width(), height());
    QRegion maskedRegion(width() / 2 - side / 2, height() /
                         2 - side / 2, side, side, QRegion::Ellipse);
    setMask(maskedRegion);
}

void Widget::mouseReleaseEvent(QMouseEvent *event){
    m_dragging=false;
    QWidget::mouseReleaseEvent(event);
}

main.cpp

#include "widget.h"
#include <QApplication>

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

    return a.exec();
}

相关标签: C++ Qt