qt绘制钟表
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);this->resize(1000,1000);this->setStyleSheet("background-color:white;");//启动计时器time_id = startTimer(1000);}void Widget::paintEvent(QPaintEvent *event)
{QPainter painter(this); //定义一个画家QPen pen;pen.setColor(QColor("pink"));//画笔颜色pen.setStyle(Qt::SolidLine);//画笔风格 实线pen.setWidth(5);painter.setPen(pen); //将画笔赋予画家实例//将中心点转移painter.translate(this->width()/2,this->height()/2);painter.drawEllipse(QPoint(0,0),360,360); //360是半径/*绘制 12个时 60个分*/pen.setColor(QColor("red"));painter.setPen(pen);//十二等分for(int i = 0;i<12;i++){painter.drawLine(0,-300, 0, -355);painter.rotate(30);}pen.setColor(QColor("black"));painter.setPen(pen);//六十等分for(int i = 0;i<60;i++){if(i%5 != 0){painter.drawLine(0,-330, 0, -355);}painter.rotate(6);}painter.rotate(30);painter.setFont(QFont("Helvetica [Cronyx]", 25, 6));for(int i = 1;i<=12;i++){painter.drawText(0, -270,QString::number(i,10));painter.rotate(30);}//秒pen.setColor(QColor("blue"));pen.setWidth(4);painter.setPen(pen);painter.rotate(sec*6);painter.drawLine(0,0, 0, -275);//分painter.rotate(-(sec*6));pen.setColor(QColor("pink"));pen.setWidth(6);painter.setPen(pen);painter.rotate((min-1)*6);painter.drawLine(0,0, 0, -200);//时painter.rotate(-(min*6));pen.setColor(QColor("red"));pen.setWidth(10);painter.setPen(pen);painter.rotate(hour*30);painter.drawLine(0,0, 0, -150);}void Widget::timerEvent(QTimerEvent *event)
{if(event->timerId() == time_id){QTime sys_t = QTime::currentTime();hour = sys_t.hour();min = sys_t.minute();sec = sys_t.second();//qDebug()<<hour<<":"<<min<<":"<<sec;无问题能显示update();}
}Widget::~Widget()
{this->killTimer(time_id);delete ui;
}