2020-8-21 QT转盘

13 篇文章 0 订阅
13 篇文章 0 订阅

2020-8-21 QT转盘

1.设定widget的大小
在这里插入图片描述
2.使用QPainter进行绘制事件
头文件:#include <QPainter>

protected:
    virtual void paintEvent(QPaintEvent* event);//增加一个绘制事件
private:
    Ui::Widget *ui;
    QPainter rotationPainter;

3.添加资源图片

增加图片可以通过右键resource.qrc选择open in Editor来插入图片

4.旋转图片

void Widget::paintEvent(QPaintEvent *event)
{
    rotationPainter.begin(this);
    rotationPainter.translate(200,200);//将坐标轴设置为200,200
    rotationPainter.rotate(10.0);
    rotationPainter.drawPixmap(-200,-200,400,400,QPixmap(":/pic/pic/choujiang.png"));
    //格式为.drawPixmap(位置x,位置y,设置图像高,设置图像宽,QPixmap("路径"))
    rotationPainter.end();
}

这样就旋转了一个小角度

5.连续旋转

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QPainter>
#include <QWidget>
#include <QEvent>
#include <QTimer>//加入计时器
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
public slots:
    void rtTimerOutSlot();//声明一个槽函数
protected:
    virtual void paintEvent(QPaintEvent* event);//增加一个绘制事件
private:
    Ui::Widget *ui;
    QPainter rotationPainter;
    QTimer rtTimer;//旋转定时器
    qreal rtAngle;//旋转角度
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    connect(&rtTimer,SIGNAL(timeout()),this,SLOT(rtTimerOutSlot()));//将成员变量reTimer与rtTimerOutSlot()槽函数关联起来
    rtTimer.start(1000);//1s旋转一次
}

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

void Widget::rtTimerOutSlot()//实现槽函数功能
{
    rtAngle++;//旋转角度加1
    update();
}

void Widget::paintEvent(QPaintEvent *event)
{
    rotationPainter.begin(this);
    rotationPainter.translate(200,200);//将坐标轴设置为200,200
    rotationPainter.rotate(rtAngle);
    rotationPainter.drawPixmap(-200,-200,400,400,QPixmap(":/pic/pic/choujiang.png"));
    //格式为.drawPixmap(位置x,位置y,设置图像高,设置图像宽,QPixmap("路径"))
    rotationPainter.end();
}

6.绘制指针

//绘制指针
pointPainter.begin(this);
pointPainter.translate(200,200);
const QPoint point[4]={QPoint(0,18),QPoint(20,0),
                      QPoint(0,-100),QPoint(-20,0)};
pointPainter.setBrush(QColor(Qt::blue));
pointPainter.drawPolygon(point,4);

//绘制钉子
//画椭圆,要先有一个四边形,然后确定椭圆
const QRect rect(-7,-7,14,18);
pointPainter.setBrush(QColor(Qt::darkYellow));
pointPainter.drawEllipse(rect);
pointPainter.end();

在这里插入图片描述
7.加入减速机制与停止机制

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QPainter>
#include <QWidget>
#include <QEvent>
#include <QTimer>//加入计时器
#include <QMouseEvent>
#include <QDebug>
#include <QTime>//随机数时间
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();
signals:
    void luckStartSignals();//发出一个开始信号
public slots:
    void rtTimerOutSlot();//声明一个槽函数
    void luckStartSlot();//开始信号对应的槽函数
protected:
    virtual void paintEvent(QPaintEvent* event);//增加一个绘制事件
    virtual void mousePressEvent(QMouseEvent *event);//增加一个鼠标点击区域事件
private:
    Ui::Widget *ui;
    QPainter rotationPainter;
    QPainter pointPainter;
    QTimer rtTimer;//旋转定时器
    int rtAngle;//旋转角度
    int randNumber;//点击停止后,目标旋转多少角度(随机数)
    int flag;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget),rtAngle(1),flag(0)
{
    ui->setupUi(this);
    connect(&rtTimer,SIGNAL(timeout()),this,SLOT(rtTimerOutSlot()));//将成员变量reTimer多久旋转一次与rtTimerOutSlot()槽函数关联起来
    connect(this,SIGNAL(luckStartSignals()),this,SLOT(luckStartSlot()));//将luckStartSignals()开始信号与luckStartSlot()开始后做什么的槽函数关联

}

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

void Widget::rtTimerOutSlot()//实现槽函数功能
{

    rtAngle++;//旋转角度加1
    if(flag%2==0)
    {
        //qDebug()<<"begin stop";
        //rtTimer.setInterval(20);//设定多少间隔一次
        if((rtAngle%720)-randNumber==90)
            rtTimer.setInterval(10);
        else if ((rtAngle%720)-randNumber==180) {
            rtTimer.setInterval(15);
        }
        else if ((rtAngle%720)-randNumber==270) {
            rtTimer.setInterval(20);
        }
        else if((rtAngle%720)-randNumber==360){
            rtAngle--;
        }
    }
        qDebug()<<rtAngle;
    update();
}

void Widget::luckStartSlot()
{
    //其实在点击停止的一瞬间已经确定了位置
    //因此撒一把随机数种子


    flag++;
    if(flag%2==0)//偶数次点击的时候停止,产生随机数
    {
        qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
        randNumber=qrand()%360;//0-359°随机
        qDebug()<<"srand="<<randNumber;

    }
    qDebug()<<"flag="<<flag;
    rtTimer.start(5);//1s旋转一次--自点击开始
}

void Widget::paintEvent(QPaintEvent *event)
{
    rotationPainter.begin(this);//表示发生在widget这个窗口
    rotationPainter.translate(200,200);//将坐标轴设置为200,200
    //平滑pixmap算法图片抗锯齿
    rotationPainter.setRenderHint(QPainter::SmoothPixmapTransform,true);//会占用一点显卡资源
    rotationPainter.rotate(rtAngle);
    rotationPainter.drawPixmap(-200,-200,400,400,QPixmap(":/pic/pic/choujiang.png"));
    //格式为.drawPixmap(位置x,位置y,设置图像高,设置图像宽,QPixmap("路径"))
    rotationPainter.end();
	
    //绘制指针
    pointPainter.begin(this);
    pointPainter.translate(200,200);
    //设置指针反锯齿
    pointPainter.setRenderHint(QPainter::Antialiasing,true);//会占用一点显卡资源
    const QPoint point[4]={QPoint(0,18),QPoint(20,0),
                          QPoint(0,-100),QPoint(-20,0)};
    pointPainter.setBrush(QColor(Qt::blue));
    pointPainter.drawPolygon(point,4);

    //绘制钉子
    //画椭圆,要先有一个四边形,然后确定椭圆
    const QRect rect(-7,-7,14,18);
    pointPainter.setBrush(QColor(Qt::darkYellow));
    pointPainter.drawEllipse(rect);
    pointPainter.end();
}

void Widget::mousePressEvent(QMouseEvent *event)//鼠标点击区域
{
    if(event->button()==Qt::LeftButton)
    {
        if(event->pos().x()>180&&event->pos().x()<220
                &&event->pos().y()>130&&event->pos().y()<216)
        {
            qDebug()<<"point event";
            emit luckStartSignals();//发出开始信号
        }
    }
}

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值