4.8作业
时钟界面
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
//定义绘制事件处理函数
void Widget::paintEvent(QPaintEvent *event)
{
//实例化一个画家类
QPainter p(this);
//定义一支笔
QPen pen;
//设置画家坐标起点
p.translate(this->width()/2,this->height()/2);
//给刷子填充颜色
p.setBrush(QBrush(QColor(255,255,255)));
//画圆形
p.drawEllipse(QPointF(0,0),200,200);
//画小时格子
pen.setWidth(3); //设置宽度
p.setPen(pen);
p.setPen(QColor(0,0,0));
for(int i=1;i<=12;i++)
{
p.rotate(30);
p.drawLine(200,0,190,0);
p.drawText(0,-170,QString("%1").arg(i));
}
//画分秒格子
p.setPen(QColor(255,0,0));
for(int i=0;i<60;i++)
{
p.drawLine(200,0,195,0);
p.rotate(6);
}
//得到当前时间
//1、通过QTime的静态成员函数currentTime获取当前的系统时间
QTime sysTime = QTime::currentTime();
//2、将系统时间转变成字符串
QString time = sysTime.toString("hh:mm:ss");
//3、将字符串拆分
int hh,mm,ss;
QStringList list = time.split(":");
hh = list[0].toUInt();
mm = list[1].toUInt();
ss = list[2].toUInt();
qDebug() <<hh<<" "<<mm<<" "<<ss;
//时间获取到后,开始运行计时器
t_id = this->startTimer(1000);
//画时分秒针
pen.setWidth(4); //设置宽度
p.setPen(pen);
p.rotate(ss*6);
p.drawLine(0,0,0,-180);
p.rotate(-ss*6);
pen.setWidth(6); //设置宽度
p.setPen(pen);
p.rotate(mm*6+ss*0.1);
p.drawLine(0,0,0,-100);
p.rotate(-mm*6-ss*0.1);
pen.setWidth(8); //设置宽度
p.setPen(pen);
p.rotate((hh%12)*30+mm*0.5+ss*0.0083);
p.drawLine(0,0,0,-40);
p.rotate(-(hh%12)*30-mm*0.50-ss*0.0083);
}
//重写定时器事件处理函数
void Widget::timerEvent(QTimerEvent *event)
{
count++;
this->update();
}