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

发布时间:2022-06-14 15:07:49 作者:iii
来源:亿速云 阅读:244

本文小编为大家详细介绍“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;
}

效果:

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

读到这里,这篇“Qt如何编写自定义控件实现抽奖转盘”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Vue.js如何实现大转盘抽奖
  2. 如何实现Jquery转盘抽奖程序

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

qt

上一篇:vue后台返回base64图片无法显示如何解决

下一篇:Python可视化调色盘如何绘制

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》