QT4.8钟表显示
前言
接上一章做了钟表优化,把指针背景显示
一、直接上源码
接上一章只用修改 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)
{
painter.save();
QImage image;
image.load("bg.jpeg");
painter.drawImage(QRect(-100,-100,this->width(),this->height()),image);
painter.restore();
/*
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();
QImage image;
image.load("red.png");
QImage mirroredImage = image.mirrored(false, true);
painter.rotate(m_hour * 30 + 30 * m_min / 60);
painter.drawImage(QRect(-6, -40, 12, 40),image);
/*
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();
QImage image;
image.load("golden.png");
QImage mirroredImage = image.mirrored(false, true);
painter.rotate(m_min * 6);
painter.drawImage(QRect(-6, -55, 12, 55),image);
/*
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();
QImage image;
image.load("black.png");
//QImage mirroredImage = image.mirrored(false, true);
painter.rotate(m_sec * 6);
painter.drawImage(QRect(-6, -70, 12, 70),image);
/*
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);
/*
int side = qMin(this->width(),this->height());
int x = this->width() - side / 2;
int y = this->height() - side / 2;
painter.setViewport(x,y,side,side);
*/
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();
//qDebug() << m_min;
drawClock(painter);
drawHour(painter);
drawMin(painter);
drawSec(painter);
}
总结
结果展示