#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPainter> //画家类
#include <QTimer> //定时器类
#include <QTime> //时间类
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void timeout_slot();
private:
Ui::Widget *ui;
//定义一个定时器变量
QTimer t1;
void paintEvent(QPaintEvent *event) override;
int h=0,m=0,s=0;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->resize(500,500);
t1.start(1000);
//由于定时器事件的信号与槽绑定
connect(&t1,&QTimer::timeout,this,&Widget::timeout_slot);
}
Widget::~Widget()
{
delete ui;
}
//绘制事件处理函数
void Widget::paintEvent(QPaintEvent *event)
{
//实例化一个画家
QPainter painter(this);
//准备画笔
QPen pen;
pen.setColor(QColor("black")); //设置颜色
pen.setWidth(3); //设置画笔粗细
//准备字体
QFont font;
font.setFamily("楷体"); //设置字体
font.setPointSize(20); //设置字号
//连接画家和画笔
painter.setPen(pen);
//给画家安排字体
painter.setFont(font);
//设置画家坐标起始点
painter.translate(this->width()/2,this->height()/2);
//绘制一个椭圆
painter.drawEllipse(QPoint(0,0),200,200);
//时针刻度
for(int i=0;i<12;i++)
{
painter.rotate(30);
painter.drawLine(QPoint(0,-180),QPoint(0,-200));
}
//分针刻度
for(int i=0;i<60;i++)
{
painter.rotate(6);
painter.drawLine(QPoint(0,-190),QPoint(0,-200));
}
//绘制刻度数字
for(int i=0;i<12;i++)
{
static int num = 1;
QString str = QString::number(num);
painter.rotate(30);
painter.drawText(QPoint(-10,-160),str);
if(num == 12)
{
num = 0;
}
num++;
}
QPainter painter1(this);
painter1.translate(this->width()/2,this->height()/2);
QPen pen1;
pen1.setColor(QColor("green"));
pen1.setWidth(3);
painter1.setPen(pen1);
painter1.rotate(s*6);
painter1.drawLine(0,0,0,-150);
QPainter painter2(this);
painter2.translate(this->width()/2,this->height()/2);
QPen pen2;
pen2.setColor(QColor("blue"));
pen2.setWidth(9);
painter2.setPen(pen2);
painter2.rotate(m*6);
painter2.drawLine(0,0,0,-100);
QPainter painter3(this);
painter3.translate(this->width()/2,this->height()/2);
QPen pen3;
pen3.setColor(QColor("red"));
pen3.setWidth(15);
painter3.setPen(pen3);
painter3.rotate(h*30);
painter3.drawLine(0,0,0,-100);
//更新界面内容
this->update();
}
void Widget::timeout_slot()
{
//获取系统的时间
QTime sysTime = QTime::currentTime();
h = sysTime.hour();
if(h>12)
{
h = h-12;
}else if(h == 24)
{
h = 0;
}
m = sysTime.minute();
s = sysTime.second();
}
DAY74
sun_over_lost2024-09-12 6:04
相关推荐
码农客栈21 分钟前
qt QWebSocketServer详解plmm烟酒僧2 小时前
Windows下QT调用MinGW编译的OpenCVBlack_Friend2 小时前
关于在VS中使用Qt不同版本报错的问题CSUC2 小时前
【Qt】QTreeView 和 QStandardItemModel的关系冷凝女子3 小时前
【QT】海康视频及openCv抓拍正脸接口苏三有春5 小时前
PyQt5实战——UTF-8编码器功能的实现(六)Vanranrr6 小时前
C++ QT兆。6 小时前
掌握 PyQt5:从零开始的桌面应用开发徒步僧17 小时前
ThingsBoard规则链节点:RPC Call Reply节点详解可峰科技18 小时前
斗破QT编程入门系列之一:认识Qt:初步使用(四星斗师)