2024.9.11 作业

发布于:2024-09-18 ⋅ 阅读:(92) ⋅ 点赞:(0)

使用绘制事件完成钟表的绘制

源文件

#include "widget.h"
#include "ui_widget.h"
#include <QDateTime>    //日期事件类
#include <QTimer>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //实例化一个定时器,每隔一秒发送timeout信号
    timer = new QTimer;
    timer->start(1000);
    connect(timer,&QTimer::timeout,[&](){
        count++;
        update();     //更新界面内容
    });
    //获取时间
    QDateTime sysdt = QDateTime::currentDateTime();
    QString t = QTime::currentTime().toString("h:m:s ap");
    QStringList list1 = t.split(" ");  //按空格分开
    QStringList list2 = list1[0].split(":");  //按冒号分开
    //得到时间的数据
    hour = list2[0].toUInt();
    minute = list2[1].toUInt();
    second = list2[2].toUInt();
    //打印验证一下
    qDebug()<<hour<<":"<<minute<<":"<<second;
}

Widget::~Widget()
{
    delete ui;


}

//绘制事件处理函数的定义
void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);           //实例化一个画家

    //准备画笔
    QPen pen;
    pen.setStyle(Qt::SolidLine);       //使用的是实线
    pen.setColor(QColor("black"));           //设置画笔颜色
    pen.setWidth(3);                   //设置画笔的粗细
    QBrush brush("pink");

    //给画家安排上画笔
    painter.setPen(pen);
    painter.setBrush(brush);  //设置背景色

    //设置画家的坐标起点
    painter.translate(this->width()/2,this->height()/2);
    //绘制一个圆
    painter.drawEllipse(QPoint(0,0), 200, 200);

    //画刻度
    for(int i=0;i<60;i++)
    {
        painter.rotate(6);
        painter.drawLine(QPoint(200,0),QPoint(195,0));
    }
    pen.setWidth(5);
    painter.setPen(pen);
    for(int i=0;i<12;i++)
    {
        painter.drawLine(QPoint(200,0),QPoint(190,0));
        painter.rotate(30);
        painter.drawText(QPoint(0,-170),QString::number(i+1));
    }

    //画时针
    pen.setWidth(10);
    pen.setColor(QColor("red"));
    painter.setPen(pen);
    painter.rotate(hour*30+6*second/60/12+30*minute/60+6*count/60/12);
    painter.drawLine(QPoint(0,-50),QPoint(0,5));



    //画分针
    QPainter painter2(this);
    painter2.translate(this->width()/2,this->height()/2);
    pen.setWidth(6);
    pen.setColor(QColor("blue"));
    painter2.setPen(pen);
    painter2.rotate(6*count/60+minute*6+6*second/60);
    painter2.drawLine(QPoint(0,-80),QPoint(0,8));


    //画秒针
    QPainter painter3(this);
    painter3.translate(this->width()/2,this->height()/2);
    pen.setWidth(3);
    pen.setColor(QColor("green"));
    painter3.setPen(pen);
    painter3.rotate(6*count+second/60);
    painter3.drawLine(QPoint(0,-120),QPoint(0,12));


}

头文件 

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPainter>
#include <QPixmap>

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:
    Ui::Widget *ui;
    QTimer *timer;
    int count = 0;
    int hour;
    int minute;
    int second;
};
#endif // WIDGET_H

 

思维导图


网站公告

今日签到

点亮在社区的每一天
去签到