2023/09/20 day4 qt

做一个动态指针钟表

头文件

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPainter>       //绘制事件类
#include <QPaintEvent>    //画家类
#include <QTime>
#include <QTimer>
#include <QTimerEvent>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    //重写绘制事件处理函数
    void paintEvent(QPaintEvent *event)override;


private slots:
    void on_startbtn_clicked();

    void timeout_slot();
private:
    Ui::Widget *ui;

    //定义一个定时器的对象指针
    QTimer *timer;
};
#endif // WIDGET_H

源文件

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //给定时器指针实例化对象
    timer = new  QTimer(this);
    connect(timer,&QTimer::timeout,this,&Widget::timeout_slot);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    //定义一个画家类
    QPainter p1(this);

    //实例化一个画笔
    QPen pen1;   //无参构造
    pen1.setColor(QColor("red"));
    pen1.setStyle(Qt::SolidLine); //设置线形,默认实线
    pen1.setWidth(5); //设置粗细
    p1.setPen(pen1);
    //直径
    int side = qMin(this->width(),this->height());
    //设置起始坐标
    p1.translate(this->width()/2,this->height()/2); //ui界面中心
    //画个圆圈当作表盘
    p1.drawEllipse(QPoint(0,0),side/2,side/2);

    //绘制刻度线
    pen1.setColor(QColor("black"));
    pen1.setWidth(3);
    p1.setPen(pen1);
    for(int i=0;i<12;i++)
    {
        p1.drawLine(0,-side/2+10,0,-side/2+20);
        p1.rotate(30);
    }

    //绘制时针、分针、秒针
    QTime curretTime = QTime::currentTime();
    //时针
    p1.setPen(Qt::NoPen);
    p1.setBrush(Qt::blue);
    p1.save();
    p1.rotate(30*(curretTime.hour()+curretTime.minute()/60));
    p1.drawConvexPolygon(QPolygon(QVector<QPoint>()<<QPoint(-2,-40)<<QPoint(2,-40)<<QPoint(4,0)<<QPoint(-4,0)));
    p1.restore();
    //分针
    p1.setBrush(Qt::black);
    p1.save();
    p1.rotate(6*(curretTime.minute() + curretTime.second() / 60));
    p1.drawConvexPolygon(QPolygon(QVector<QPoint>() << QPoint(-1, -50) << QPoint(1, -50) << QPoint(1, 100) << QPoint(-1, 100)));
    p1.restore();

    //秒针
    p1.setPen(Qt::red);
    p1.setBrush(Qt::red);
    p1.save();
    p1.rotate(6* curretTime.second());
    p1.drawConvexPolygon(QPolygon(QVector<QPoint>() << QPoint(-1, -70) << QPoint(1, -70) << QPoint(0, 160) << QPoint(-0, 160)));
    p1.restore();
}



//启动定时器按钮对应的槽函数
void Widget::on_startbtn_clicked()
{
    timer->start(1000);
}

void Widget::timeout_slot()
{
    update();
}

效果图

相关推荐
程序猿追6 小时前
kotlin-retry 接入鸿蒙后,怎样确认它真的会停止重试
开发语言·kotlin·harmonyos
我是人✓6 小时前
Java文件上传三方式对比:从传统到SpringBoot
java·开发语言
智能直播6 小时前
网络RTMP拉流不卡顿、声音不同步,一文讲透缓冲区、时间戳
开发语言·ffmpeg·php·音视频·视频编解码
墨云安全6 小时前
内网资产无法暴露到公网,怎么做安全检测?2026 年内网渗透测试的落地路径
开发语言·安全·网络安全·渗透测试·php
摇滚侠7 小时前
《计算机是如何工作的:人人都能懂的计算机软硬件工作原理》第 9 章 机器码与汇编语言 阅读笔记 9
java·开发语言·笔记
Cc.Y7 小时前
Java零基础入门:深入Java核心:文件IO与序列化全解
java·开发语言·python
jimy18 小时前
c++的 Type Traits (一)
开发语言·c++
Web极客码9 小时前
Python Ollama 本地 Agent 入门与订单查询实践
开发语言·jvm·python
kimnoic9 小时前
PHP 8.2的DNF类型怎么用才规范
开发语言·php
_Evan_Yao10 小时前
前后端联调怎么排查问题?从 Network 到后端日志,一步步定位错误
开发语言·后端·团队开发·需求分析