Qt编写自定义控件实现抽奖转盘

 更新时间:2022年06月14日 11:42:55   作者:友善啊,朋友  
这篇文章主要为大家详细介绍了Qt编写自定义控件实现抽奖转盘,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Qt自定义控件实现抽奖转盘的具体代码,供大家参考,具体内容如下

#ifndef LOTTERYTURNTABLEWIDGET_H
#define LOTTERYTURNTABLEWIDGET_H
 
#include <QWidget>
 
class LotteryTurntableWidget : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(int rotate READ getRotate WRITE setRotate MEMBER painterRotate)
public:
    LotteryTurntableWidget(QWidget *parent = nullptr);
    ~LotteryTurntableWidget()override;
    int getRotate();
    void setRotate(int rotate);
 
protected:
    void paintEvent(QPaintEvent *event)override;
    void mousePressEvent(QMouseEvent *event)override;
    void mouseReleaseEvent(QMouseEvent *event)override;
 
private:
    QRect centerBtnRect;
    bool isPressCenterBtn{false};
    bool isRuning{false};
    int painterRotate{0};
    void onRotateFinished();
    QList<Qt::GlobalColor> colorList;
};
#endif // LOTTERYTURNTABLEWIDGET_H
#include "lotteryturntablewidget.h"
#include <QPainter>
#include <QPaintEvent>
#include <QPainterPath>
#include <QTime>
#include <QDebug>
#include <QRandomGenerator>
#include <QPropertyAnimation>
 
LotteryTurntableWidget::LotteryTurntableWidget(QWidget *parent)
    : QWidget(parent)
{
    setPalette(Qt::white);
    setMinimumSize(500,500);
 
    colorList << Qt::red << Qt::yellow << Qt::green << Qt::cyan << Qt::blue << Qt::magenta << Qt::darkGreen << Qt::darkCyan;
}
 
LotteryTurntableWidget::~LotteryTurntableWidget()
{
}
 
int LotteryTurntableWidget::getRotate()
{
    return painterRotate;
}
 
void LotteryTurntableWidget::setRotate(int rotate)
{
    painterRotate = rotate;
    update();
}
 
void LotteryTurntableWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,true);  //反走样开启
    const auto rect = event->rect();
    auto radius = std::min(rect.width(),rect.height()) / 2 - 25;
 
    painter.save();
    painter.translate(rect.center()); //将坐标系的原点设置为(r,r)
 
    QPen pen;
    pen.setColor(QColor("#F0630B"));
    pen.setWidth(16);
    painter.setPen(pen);
    painter.drawEllipse(QPoint(0, 0), radius, radius);
 
    pen.setColor(QColor("#FF4500"));
    pen.setWidth(8);
    painter.setPen(pen);
    radius -= 8;
    painter.drawEllipse(QPoint(0, 0), radius, radius);
 
    pen.setColor(QColor("#B71606"));
    pen.setWidth(40);
    painter.setPen(pen);
    radius -= 24;
    painter.drawEllipse(QPoint(0, 0), radius, radius);
 
    painter.save();
    if(!isRuning)
    {
        painter.setPen(Qt::white);
        painter.setBrush(Qt::white);
    }
    for (int i = 0; i < 20; ++i)
    {
        painter.rotate(18.0);
        int smallEllipse;
        if(i % 2 == 0)
        {
            if(isRuning)
            {
                if(painterRotate % 2 == 0)
                {
                    painter.setPen(Qt::red);
                    painter.setBrush(Qt::red);
                }
                else
                {
                    painter.setPen(Qt::blue);
                    painter.setBrush(Qt::blue);
                }
            }
            smallEllipse = 15;
        }
        else
        {
            if(isRuning)
            {
                if(painterRotate % 2 == 0)
                {
                    painter.setPen(Qt::blue);
                    painter.setBrush(Qt::blue);
                }
                else
                {
                    painter.setPen(Qt::red);
                    painter.setBrush(Qt::red);
                }
            }
            smallEllipse = 10;
        }
        painter.drawEllipse(QPoint(radius, 0), smallEllipse, smallEllipse);
    }
    painter.restore();
 
    pen.setColor(QColor("#FFC228"));
    pen.setWidth(20);
    painter.setPen(pen);
    radius -= 30;
    painter.drawEllipse(QPoint(0, 0), radius, radius);
 
    radius -= 10;
    auto centerRect = QRect(-radius,-radius,radius * 2,radius * 2);
 
    painter.setPen(Qt::transparent);
    painter.save();
    painter.rotate(18.0 * painterRotate);
    for (int i = 0;i < 8;++i)
    {
        QPainterPath path;
        path.moveTo(0,0);
        path.arcTo(centerRect, 45 * i,45);
        path.closeSubpath();
        painter.fillPath(path,colorList[i]);
    }    
    painter.restore();
 
    QPainterPath trianglePath;//三角形
    QPolygon polygon;
    polygon.append(QPoint(0,-radius * 0.55));
    polygon.append(QPoint(-radius * 0.25,0));
    polygon.append(QPoint(radius * 0.25,0));
    trianglePath.addPolygon(polygon);
    painter.setBrush(QColor("#EEDAA2"));
    painter.drawPath(trianglePath);
 
    painter.setBrush(QColor("#FDFAEA"));
    radius = static_cast<int>(radius * 0.3);
    painter.drawEllipse(QPoint(0, 0), radius, radius);
 
    painter.setBrush(isPressCenterBtn ? QColor("#B91A0D").lighter() : QColor("#B91A0D"));//中间的按钮
    radius -= 2;
    painter.drawEllipse(QPoint(0, 0), radius, radius);
 
    centerBtnRect = QRect(rect.width() / 2 - radius,rect.height() / 2 - radius,radius * 2,radius * 2);
    painter.restore();
}
 
void LotteryTurntableWidget::mousePressEvent(QMouseEvent *event)
{
    if(isRuning)
    {
        QWidget::mousePressEvent(event);
        return;
    }
    QRegion ellipseRegion(centerBtnRect, QRegion::Ellipse);
    isPressCenterBtn = ellipseRegion.contains(event->pos());
    if(isPressCenterBtn)
    {
        isRuning = true;
 
        QPropertyAnimation *animation = new QPropertyAnimation(this, "rotate");
        animation->setEasingCurve(QEasingCurve::InOutCubic);
        animation->setDuration(3000);
        animation->setStartValue(0);
        animation->setEndValue(QRandomGenerator::global()->bounded(360) + 360 * 5);
        connect(animation, &QAbstractAnimation::finished, this, &LotteryTurntableWidget::onRotateFinished);
        animation->start(QAbstractAnimation::DeleteWhenStopped);
        update();
    }
    QWidget::mousePressEvent(event);
}
 
void LotteryTurntableWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if(isPressCenterBtn)
    {
        isPressCenterBtn = false;
        update();
    }
    QWidget::mouseReleaseEvent(event);
}
 
void LotteryTurntableWidget::onRotateFinished()
{
    isRuning = false;
}

效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • OpenCV4.1.0+VisualStudio2019开发环境搭建(超级简单)

    OpenCV4.1.0+VisualStudio2019开发环境搭建(超级简单)

    这篇文章主要介绍了OpenCV4.1.0+VisualStudio2019开发环境搭建(超级简单),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • C++中智能指针最常用的shared_ptr和unique_ptr

    C++中智能指针最常用的shared_ptr和unique_ptr

    C++中的智能指针最常用的是shared_ptr和unique_ptr,C++新手最常问的问题是我从一个函数中拿到unique_ptr,但要转成shared_ptr才能使用,要怎么转换?同理是否能将shared_ptr转换成unique_ptr,面对这些问题,跟随小编一起看看吧
    2022-08-08
  • C语言中函数栈帧的创建和销毁的深层分析

    C语言中函数栈帧的创建和销毁的深层分析

    在C语言中,每一个正在运行的函数都有一个栈帧与其对应,栈帧中存储的是该函数的返回地址和局部变量。从逻辑上讲,栈帧就是一个函数执行的环境:函数参数、函数的局部变量、函数执行完后返回到哪里等等
    2022-04-04
  • C#委托所蕴含的函数指针概念详细解析

    C#委托所蕴含的函数指针概念详细解析

    C#中用委托这种概念实现了函数指针技术而已,另外.ent提供额外的安全性,当然也损失了灵活性
    2013-09-09
  • Qt实现图片移动实例(图文教程)

    Qt实现图片移动实例(图文教程)

    这学期实训的时候用MFC做过一个飞机大战,很无聊的东西,一直想用Qt做一个;首先需要解决的问题是图片的移动,怎么说飞机啊子弹啊都是动着的,图片当然要跑起来,感兴趣的你可不要走开啊
    2013-01-01
  • C++模拟实现vector流程详解

    C++模拟实现vector流程详解

    这篇文章主要介绍了C++容器Vector的模拟实现,Vector是一个能够存放任意类型的动态数组,有点类似数组,是一个连续地址空间,下文更多详细内容的介绍,需要的小伙伴可以参考一下
    2022-08-08
  • C语言中编写可变参数函数

    C语言中编写可变参数函数

    这篇文章主要介绍了C语言中编写可变参数函数的相关资料,需要的朋友可以参考下
    2017-07-07
  • C语言实现动态顺序表的实现代码

    C语言实现动态顺序表的实现代码

    这篇文章主要介绍了C语言实现动态顺序表的实现代码的相关资料,动态顺序表在内存中开辟一块空间,可以随我们数据数量的增多来扩容,需要的朋友可以参考下
    2017-08-08
  • 基于c++的中国象棋游戏设计与实现

    基于c++的中国象棋游戏设计与实现

    这篇文章主要介绍了基于c++的中国象棋游戏设计与实现,主要操作是possibleMove(int x, int y),通过整个棋盘每个位置上的信息、中国象棋的规则来获得位置(x, y)这个棋子可以移动到的位置,需要的朋友可以参考一下
    2022-02-02
  • 使用UART与PC通信实现msp430g2553单片机超声波测距示例

    使用UART与PC通信实现msp430g2553单片机超声波测距示例

    这篇文章主要介绍了使用UART与PC通信实现msp430g2553单片机超声波测距示例,需要的朋友可以参考下
    2014-05-05

最新评论