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