Qt之简易转盘抽奖「建议收藏」

Qt之简易转盘抽奖「建议收藏」额,忘记了哪位哲人说过:“站在巨人的肩膀上,我们能看得更高!”嗯,大概就是这个意思了。这两天学习“一去二三里”大神博客里的Qt绘图事件,其中有一篇涉及到画圆盘,突然有想法,写个简易的抽奖小demo。了解了这种抽奖的原理和机制后,马丹,幸好没有买过各种彩票之类的,各种奖项的概率值分明是设置出来的嘛(我程序里是写死了)..好,先截个图:简易能用。。哈哈头文件代码如下:#ifndefWIDGET

  • 额,忘记了哪位哲人说过:“站在巨人的肩膀上,我们能看得更高!”嗯,大概就是这个意思了。这两天学习“一去二三里”大神博客里的Qt绘图事件,其中有一篇涉及到画圆盘,突然有想法,写个简易的抽奖小demo。了解了这种抽奖的原理和机制后,马丹,幸好没有买过各种彩票之类的,各种奖项的概率值分明是设置出来的嘛(我程序里是写死了)..好,先截个图:

choujaing

  • 简易能用。。哈哈
  • 头文件代码如下:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPainter>
#include <QRadialGradient>
#include <QPainterPath>
#include <QTimer>
#include <QDebug>
#include <QMouseEvent>
#include <QPushButton>
#include <QTime>
#include <QLabel>
#include <QLineEdit>
#include <QMap>
#include <QMapIterator>
#define MAX_CIRCLE 1800
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
    Q_OBJECT
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    void gradientArc(QPainter *painter,int radius,int startAngle,
                     int angleLength,int arcHeight,QRgb color);
public slots:
    void updatePaint();
    void btnClicked();
    void reResult(int re);
    void stopRotate();
signals:
    void sigResult(int re);
private:
    Ui::Widget *ui;
    int m_nRotationAngle;
    int m_nRo;
    quint32 m_T;
    QString m_Re;
    QTimer *m_timer;
    QTimer *timer;
    int m_i;
    QPushButton *m_btn;
    bool isDefault;
    enum Awards{
        Spe = 3,
        First = 7,
        Second = 5,
        Third = 2,
        Luck_Fir = 6,
        Luck_Sec = 1,
        ThanK_Fir = 4,
        Thank_Sec = 0,
    };
    Awards m_award;
    QLabel *m_labTr;
    QLabel *m_labRe;
    QLineEdit *m_ldeRe;
    QMap<int,QString> m_map;
    QMap<int, QString> returnResult(int re);
    static QString showAwards(Awards award);
    void stopInit();
    int getRand();
protected:
    void paintEvent(QPaintEvent *);
};
#endif // WIDGET_H
  • 实现文件代码如下:
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    resize(600,560);
    setMouseTracking(true);
    m_nRotationAngle = 0;
    m_i = 0;
    m_T = 0;
    isDefault = false;
    m_btn = new QPushButton(this);
    m_btn->move(273,450);
    m_labTr = new QLabel(tr("抽奖结果:"),this);
    m_labTr->move(223,105);
    m_ldeRe = new QLineEdit(tr("未开始抽奖"),this);
    m_ldeRe->move(293,103);
    m_ldeRe->setEnabled(false);
    m_ldeRe->setAlignment(Qt::AlignCenter);
    m_btn->setText(tr("启动"));
    m_timer = new QTimer(this);
    setStyleSheet("QLabel{"
                  "font-size:15px;"
                  ""
                  "}"
                  "QLineEdit{"
                  "color:white;"
                  "font-size:14px;"
                  "background-color:#f57ab8;"
                  "border:1px solid gray;"
                  "border-radius:5px;"
                  "}"
                  "QPushButton{"
                  "width:36px;"
                  "height:16px;"
                  "text-align:center;"
                  "background-color:#da0000;"
                  "color:white;"
                  "border:1px solid gray;"
                  "border-radius:3px;"
                  "}"
                  );
    connect(m_timer,SIGNAL(timeout()),this,SLOT(updatePaint()));
    connect(m_btn,SIGNAL(clicked(bool)),this,SLOT(btnClicked()));
    connect(this,SIGNAL(sigResult(int)),this,SLOT(reResult(int)));
}

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

void Widget::gradientArc(QPainter *painter, int radius, int startAngle,
                         int angleLength, int arcHeight, QRgb color)
{
    QRadialGradient gradient(0,0,radius);
    gradient.setColorAt(0,Qt::black);
    gradient.setColorAt(1.0,color);
    painter->setBrush(gradient);

    // << 1(左移1位)相当于radius*2 即:150*2=300
    //QRectF(-150, -150, 300, 300)
    QRectF rectf(-radius,-radius,radius << 1,radius << 1);
    QPainterPath path;
    path.arcTo(rectf,startAngle,angleLength);

    QPainterPath subPath;
    subPath.addEllipse(rectf.adjusted(arcHeight, arcHeight, -arcHeight, -arcHeight));
    //subPath.addEllipse(rect.adjusted(arcHeight,arcHeight,-arcHeight,-arcHeight));
    //path为扇形 subPath为椭圆
    path -= subPath;//

    // QFont font;
    // font.setFamily("Microsoft YaHei");
    // font.setPointSize(14);
    // painter->setPen(Qt::NoPen);
    // path.addText(path.pointAtPercent(0.5), font, QStringLiteral("一去丶二三里"));
    painter->setPen(Qt::NoPen);
    painter->drawPath(path);
}

void Widget::updatePaint()
{
    if(!isDefault)
    {
        m_nRotationAngle = m_nRotationAngle + 33;
        if(m_nRotationAngle >360)
        {
            m_nRotationAngle = 0;
            m_T++;
            if(m_T == 3)
            {
                m_nRotationAngle = m_nRotationAngle + 5;

            }else if(m_T == 5)
            {
                 stopInit();
            }
        }
    }
    else
    {
        m_nRotationAngle = m_nRotationAngle + 33;
        if(m_nRotationAngle >360)
        {
            m_nRotationAngle = 0;
            m_T++;
            if(m_T == 3)
            {
                m_nRotationAngle = m_nRotationAngle + 5;

            }else if(m_T == 5)
            {
                 stopInit();
            }
        }
    }
    update();
}

void Widget::btnClicked()
{
    if(!m_timer->isActive())
    {
        m_timer->start(30);
        int iRangd = getRand();
        emit sigResult(iRangd);
        m_T = 0;
        m_ldeRe->setStyleSheet("color:white;");
        m_ldeRe->setText(tr("正在抽奖..."));
    }else
    {
        stopInit();
    }
}

void Widget::reResult(int re)
{
    isDefault = true;
    m_map =  returnResult(re);
    QMapIterator<int,QString> ii(m_map);
    if(m_map.isEmpty())
        return;
    while(ii.hasNext())
    {
        ii.next();
        m_Re = ii.value();
        m_nRo = ii.key();
    }
}

void Widget::stopRotate()
{
    m_timer->stop();
}

QMap<int,QString> Widget::returnResult(int re)
{

    int ire = 0;
    QMap<int,QString> map;
    switch(re)
    {
    case 0:
        //(rand()%(b-a))+ a,-->[a,b)的随机数
        ire = (qrand() % (45 - 0) + 0);
        qDebug() << "二等奖";
        map.insert(ire,tr("二等奖"));
        break;
    case 1:
        qDebug() << "谢谢1";
        ire = (qrand() % (90 - 46) + 46);
        map.insert(ire,tr("谢谢参与"));
        break;
    case 2:
        qDebug() << "特等奖";
        ire = (qrand() % (135 - 90) + 90);
        map.insert(ire,tr("特等奖"));
        break;
    case 3:
        qDebug() << "三等奖";
        ire = (qrand() % (180 - 135) + 135);
        map.insert(ire,tr("三等奖"));
        break;
    case 4:
        qDebug() << "幸运2";
        ire = (qrand() % (225 - 180) + 180);
        map.insert(ire,tr("幸运奖"));
        break;
    case 5:
        qDebug() << "谢谢2";
        ire = (qrand() % (270 - 225) + 225);
        map.insert(ire,tr("谢谢参与"));
        break;
    case 6:
        qDebug() << "一等奖";
        ire = (qrand() % (315 - 270) + 270);
        map.insert(ire,tr("一等奖"));
        break;
    case 7:
        qDebug() << "幸运1";
        ire = (qrand() % (360 - 315) + 315);
        map.insert(ire,tr("幸运奖"));
        break;
    default:
        break;
    }
    return map;
}

QString Widget::showAwards(Widget::Awards award)
{
    QString result = "";
    switch(award)
    {
    case Spe:
        result = "特等奖";
        break;
    case First:
        result = "一等奖";
        break;
    case Second:
        result = "二等奖";
        break;
    case Third:
        result = "三等奖";
        break;
    case Luck_Fir:
        result = "幸运奖";
        break;
    case Luck_Sec:
        result = "幸运奖";
        break;
    case ThanK_Fir:
        result = "谢谢参与";
        break;
    case Thank_Sec:
        result = "谢谢参与";
        break;
    default:
        break;
    }
    return result;
}

void Widget::stopInit()
{
    m_timer->stop();
    m_T = 0;
    isDefault = false;
    int ip =  m_nRo;
    int tp = m_nRotationAngle;
    if(ip > m_nRotationAngle)
    {
        for (int i = tp; i <= ip; i++)
        {
            m_nRotationAngle = i;
        }
    }
    m_nRotationAngle = m_nRo;
    m_ldeRe->setText(m_Re);
    update();
}

//设置奖项概率
int Widget::getRand()
{
    int re = 10;
    QTime tim = QTime::currentTime();
    qsrand(tim.msec() + tim.second()*1000);
    int rand = ((qrand() % 100) + 1);
    qDebug() << "rand:" << rand;
    if(rand == 1)
    {
        re = 2;
    }else if((rand <= 4) && (rand >= 2 ))
    {
        re = 6;
    }else if((rand <= 9) && (rand >= 5 ))
    {
        re = 0;
    }else if((rand <= 20) && (rand >= 10 ))
    {
        re = 3;
    }else if((rand <= 35) && (rand >= 21 ))
    {
        re = 4;
    }else if((rand <= 50) && (rand >= 36 ))
    {
        re = 7;
    }else if((rand <= 75) && (rand >= 51 ))
    {
        re = 1;
    }else if((rand <= 100) && (rand >= 76 ))
    {
        re = 5;
    }
    return re;
}

void Widget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,true);
    int radius = 150;
    int arcHeight = 138;

    //>>1 右移一位,相当于width() / 2
    painter.translate(width() >> 1,height() >> 1);

    // painter.rotate(m_nRotationAngle);

    /* * 参数二:半径 * 参数三:开始的角度 * 参数四:指扫取的角度-顺时针(360度 / 8 = 45度) * 参数五:圆环的高度 * 参数六:填充色 **/
    gradientArc(&painter,radius,0,45,arcHeight,qRgb(200,200,0));
    gradientArc(&painter,radius,45,45,arcHeight,qRgb(200,0,200));
    gradientArc(&painter,radius,90,45,arcHeight,qRgb(0,200,200));
    gradientArc(&painter,radius,135,45,arcHeight,qRgb(200,0,0));
    gradientArc(&painter,radius,225,45,arcHeight,qRgb(0,200,0));
    gradientArc(&painter,radius,180,45,arcHeight,qRgb(0,0,200));
    gradientArc(&painter,radius,270,45,arcHeight,qRgb(0,0,0));
    gradientArc(&painter,radius,315,45,arcHeight,qRgb(150,150,150));

    painter.setPen(QPen(QColor(Qt::yellow),2));
    painter.rotate(-35);
    painter.drawText(60,30,tr("谢谢参与"));

    painter.rotate(40);
    painter.drawText(70,30,tr("一等奖"));

    painter.rotate(95);
    painter.drawText(70,30,tr("二等奖"));

    painter.rotate(135);
    painter.drawText(70,30,tr("三等奖"));

    painter.rotate(180);
    painter.drawText(70,30,tr("幸运奖"));

    painter.rotate(210);
    painter.drawText(70,30,tr("幸运奖"));

    painter.rotate(245);
    painter.drawText(70,30,tr("谢谢参与"));

    painter.rotate(40);
    painter.drawText(70,30,tr("特等奖"));

    QPainter painter2(this);
    painter2.setRenderHint(QPainter::Antialiasing,true);
    painter2.translate(width() >> 1,height() >> 1);
    painter2.rotate(m_nRotationAngle);
    static const QPoint poit[4] = {QPoint(0,-18),QPoint(10,0),QPoint(0,60),QPoint(-10,0)};
    painter2.setBrush(QColor(Qt::red));
    painter2.setPen(Qt::NoPen);
    painter2.drawPolygon(poit,4);

    painter2.setBrush(QColor(Qt::yellow));
    painter2.drawEllipse(-7,-7,14,14);
}
  • 我自己写的代码比较烂,各位看官嘴下留情…一直想实现的缓动效果,就是当针快要停下的时候缓慢的停下来。说是需要添加额外的库。水平有限,以后有机会再慢慢实现。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/10597.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注