QT入门开发一个时钟( 二 )


.cpp
#include "mainwindow.h"#include #include #include #include #include #define PI 3.1415926MainWindow::MainWindow(QWidget *parent): QMainWindow(parent),fTimer(new QTimer(this)){resize(WIN_WIDTH,WIN_HEIGHT);setFixedSize(WIN_WIDTH,WIN_HEIGHT);m_centX = WIN_WIDTH / 2 ;// 计算中心位置m_centY = WIN_HEIGHT / 2;// 计算中心位置m_distance = WIN_WIDTH / 2 - 50;// 计算半径距离fTimer->stop();fTimer->setInterval (500) ;//设置定时周期,单位:毫秒,半秒刷新一次 。connect(fTimer,SIGNAL(timeout()),this,SLOT(on_timer_timeout()));fTimer->start();on_timer_timeout(); // 更新时间update();}MainWindow::~MainWindow(){}void MainWindow::on_timer_timeout(){fTimeCounter = QTime::currentTime();m_min = fTimeCounter.minute();m_second = fTimeCounter.second();m_hour = fTimeCounter.hour();update();}void MainWindow::paintEvent(QPaintEvent *event){QPainter painter(this);drawBackground(&painter);// 更新背景drawHourHand(&painter);// 更新时针drawMinuteHand(&painter);// 更新分针drawSecondHand(&painter);// 更新秒针}void MainWindow::drawBackground(QPainter *painter){int z12 = QFontMetrics(this->font()).width("12");// 计算两个数字的长度int ifontWidth = QFontMetrics(this->font()).width("1");// 计算一个数字的长度int ifontHeight = QFontMetrics(this->font()).height();// 一个字的高度int ihalfWidth = QFontMetrics(this->font()).width("1")/2;int ihalfHeight = QFontMetrics(this->font()).height()/2;// 半个字int distace = static_cast(m_distance*sqrt(3)/2);// 根号3/2的距离、// 绘制数字painter->setFont(this->font());painter->drawText(m_centX - z12/2,m_centY-m_distance + ihalfHeight,"12");painter->drawText(m_centX + m_distance/2 - ihalfWidth,m_centY - distace - ihalfHeight,ifontWidth,ifontHeight,1,"1");painter->drawText(m_centX + distace - ihalfWidth,m_centY - m_distance/2- ihalfHeight,ifontWidth,ifontHeight,1 ,"2");painter->drawText(m_centX + m_distance- ihalfWidth,m_centY - ihalfHeight,ifontWidth,ifontHeight,1,"3");painter->drawText(m_centX + distace - ihalfWidth,m_centY + m_distance/2 - ihalfHeight,ifontWidth,ifontHeight,1,"4");painter->drawText(m_centX + m_distance/2 - ihalfWidth,m_centY + distace -ihalfHeight,ifontWidth,ifontHeight,1,"5");painter->drawText(m_centX - ihalfWidth,m_centY + m_distance - ihalfHeight,ifontWidth,ifontHeight,1,"6");painter->drawText(m_centX - m_distance/2 - ihalfWidth,m_centY + distace -ihalfHeight,ifontWidth,ifontHeight,1,"7");painter->drawText(m_centX - distace - ihalfWidth,m_centY + m_distance/2- ihalfHeight,ifontWidth,ifontHeight,1,"8");painter->drawText(m_centX - m_distance - ihalfWidth,m_centY - ihalfHeight,ifontWidth,ifontHeight,1,"9");painter->drawText(m_centX - distace - z12/2,m_centY - m_distance/2- ihalfHeight,z12,ifontHeight,2,"10");painter->drawText(m_centX - m_distance/2 - z12/2,m_centY-distace - ihalfHeight,z12,ifontHeight,2,"11");qreal VAR_PI = 180/M_PI;// 绘制最外圈原点for(int i = 0;i<60 ; i++){int x = m_centX + static_cast(sin(6*i/VAR_PI)*(m_distance + 20));// sin按照弧度计算,因此需要做角度与弧度计算,例如:30 (度) =30/180 * 3.1415926 (弧度)int y = m_centY - static_cast(cos(6*i/VAR_PI)*(m_distance + 20));QPoint p(x,y);painter->setBrush(QBrush(Qt::black,Qt::SolidPattern));if( i%5 == 0 ){painter->drawEllipse(p,4,4);}else{painter->drawEllipse(p,2,2);}}}void MainWindow::drawHourHand(QPainter *painter){QPixmap img(":/4.png"); // 读取资源文件QPixmap img2 = rotateImageWithTransform(img,30*(m_hour + 6*m_min/360));// 按照中心旋转painter->drawPixmap(m_centX-img2.width()/2,m_centY-img2.height()/2,img2);}void MainWindow::drawMinuteHand(QPainter *painter){QPixmap img;img.load(":/3.png");QPixmap img2 = rotateImageWithTransform(img,6*m_min);painter->drawPixmap(m_centX-img2.width()/2,m_centY-img2.height()/2,img2);}void MainWindow::drawSecondHand(QPainter *painter){QPixmap img;img.load(":/2.png");QPixmap img2 = rotateImageWithTransform(img,6*m_second);painter->drawPixmap(m_centX-img2.width()/2,m_centY-img2.height()/2,img2);}QPixmap MainWindow::rotateImageWithTransform(const QPixmap &src, int angle){QMatrix matri;//迁移到中心matri.translate(src.width()/2.0,src.height()/2.0);//中心旋转matri.rotate(angle);//回退中心matri.translate(-src.width()/2.0,-src.height()/2.0);//执行坐标映射变化//旋转后图像大小变化了 需要提前进行裁剪 如果在旋转后裁剪//则需要计算使用三角函数计算//中心偏移int cubeWidth = qMin(src.width(),src.height());QRect cubeRect(0,0,cubeWidth,cubeWidth);cubeRect.moveCenter(src.rect().center());qInfo()