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();
}

效果图

相关推荐
野生技术架构师19 分钟前
高级 Java 工程师面试八股(2026 精选版)
java·开发语言·面试
小小-猿19 分钟前
用 Python 写昇腾算子:pyasc 是怎么回事
开发语言·python·昇腾·cann·pyasc
星夜离尘5622 分钟前
MATLAB:标定表局部倒退,为什么不能排序?
android·开发语言·matlab
智购科技智能售货柜26 分钟前
自动售货机步进电机细分驱动实战:力矩、噪声与堵转~YH
大数据·开发语言·人工智能·科技·嵌入式硬件
蓝蓝的代码人1 小时前
用 Python、Qt 和 Vue 做一个 Windows 股票看盘工具,观潮的行情小窗与桌面提醒
vue.js·python·qt·开源·桌面应用
杜 硕1 小时前
从零开始的C++生活——第十五讲
开发语言·数据结构·c++·生活
sunly_1 小时前
Go 切片详解
开发语言·oracle·golang
fengkai45451 小时前
Python对接LangChain智能体开发(Tool工具调用与 Agent智能体)
运维·开发语言·python·langchain·llm·ai agent·tool
随流寻影2 小时前
利用 C++ 实现二叉搜索树的增删查
开发语言·c++
总要学点什么3 小时前
第128篇:镜头光晕与高光——把逆光太阳晕成会躲山的光斑
开发语言·javascript·数码相机·microsoft·材质·cesium