QT4.8制作时钟1.0

QT4.8制作时钟1.0

简易版的钟表



前言

这一篇加入源码制作简单的钟表,主要锻炼QPainter绘制功能的能力


一、介绍

主要是对QPainter接口调用,熟悉画笔和刷子的使用,下一章将会对界面进行优化

二、源码

main.c

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Clock w;
    w.show();
    
    return a.exec();
}

clock.h

#ifndef CLOCK_H
#define CLOCK_H

#include <QWidget>
#include <QTimer>
#include <QPainter>
#include <QColor>
#include <QPen>
#include <QBrush>
#include <QDebug>
#include <QTime>

namespace Ui {
class Clock;
}

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

    void drawClock(QPainter &painter);
    void drawHour(QPainter &painter);
    void drawMin(QPainter &painter);
    void drawSec(QPainter &painter);

public:
    void paintEvent(QPaintEvent *);

private:
    Ui::Clock *ui;

    int m_hour;
    int m_min;
    int m_sec;
    QTimer *m_timer;
    QPoint m_hourHand[4];
    QPoint m_minuteHand[4];
    QPoint m_secondHand[4];
    QPen m_HandPen;
};

#endif // CLOCK_H

clock.cpp

#include "clock.h"
#include "ui_clock.h"

Clock::Clock(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Clock)
{
    ui->setupUi(this);
    this->setFixedSize(200,200);

    m_timer = new QTimer(this);
    connect(m_timer,SIGNAL(timeout()),this,SLOT(update()));
    m_timer->start(1000);
}

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

void Clock::drawClock(QPainter &painter)
{
    m_HandPen.setColor(Qt::black);
    m_HandPen.setCapStyle(Qt::RoundCap);

    painter.save();
    for(int i = 0; i <= 60; i++){
        if(i % 5 == 0){
            painter.setPen(m_HandPen);
            painter.drawLine(0, 70, 0, 90);
        }else{
            painter.setPen(m_HandPen);
            painter.drawLine(0, 85, 0, 90);
        }
         painter.rotate(6);
    }
    painter.restore();
}

void Clock::drawHour(QPainter &painter)
{
    painter.save();

    QPen pen;
    QColor pointerSecPenColor(255,0,0);
    pen.setCapStyle(Qt::RoundCap);
    painter.setPen(pointerSecPenColor);
    painter.setBrush(pointerSecPenColor);

    QPolygon pts;
    pts.setPoints(3, -1, 10, 1, 10, 0, -50);

    painter.rotate(m_hour *  30);
    painter.drawConvexPolygon(pts);

    painter.restore();
}

void Clock::drawMin(QPainter &painter)
{
    painter.save();

    QPen pen;
    QColor pointerSecPenColor(255,106,106);
    pen.setCapStyle(Qt::RoundCap);
    painter.setPen(pointerSecPenColor);
    painter.setBrush(pointerSecPenColor);

    QPolygon pts;
    pts.setPoints(3, -1, 10, 1, 10, 0, -60);

    painter.rotate(m_min * 6);
    painter.drawConvexPolygon(pts);
    painter.restore();
}

void Clock::drawSec(QPainter &painter)
{
    painter.save();

    QPen pen;
    QColor pointerSecPenColor(127, 0, 127);
    pen.setCapStyle(Qt::RoundCap);
    painter.setPen(pointerSecPenColor);
    painter.setBrush(pointerSecPenColor);

    QPolygon pts;
    pts.setPoints(3, -1, 10, 1, 10, 0, -70);

    painter.rotate(m_sec * 6);
    painter.drawConvexPolygon(pts);

    painter.restore();
}

void Clock::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    painter.translate(this->width() / 2, this->height() / 2);

    QTime current_time = QTime::currentTime();

    m_hour = current_time.hour();
    m_min = current_time.minute();
    m_sec = current_time.second();

    drawClock(painter);
    drawHour(painter);
    drawMin(painter);
    drawSec(painter);
}


显示

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_小白鱼儿_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值