qt qpixmap save一直返回 false_Qt转盘抽奖

3de2d780d49fc0a4a0e2f836c9408fcf.gif

效果图

c68798811ca1e61acf69b84bf6209afa.gif

fcf2ef02fa630e1fed1624e333ef262d.gif

cdbe28a18eb4852a8b614414b6c72a3e.gif

核心代码

#include "lotterydemo.h"
#include <QPainter>
#include <QPixmap>
#include <QTime>
#include <QMouseEvent>
#include <QDebug>
#include <QPropertyAnimation>
#include <QMessageBox>
#include "commonmaskwidget.h"

QString LottertData[] = { QStringLiteral("Iphone手机"), QStringLiteral("IPAD平板"), QStringLiteral("IPAD平板"), QStringLiteral("上海迪士尼门票"), QStringLiteral("上海迪士尼门票"),
QStringLiteral("剑南春酒文化之旅"), QStringLiteral("剑南春酒文化之旅"), QStringLiteral("古井酒文化之旅"), QStringLiteral("古井酒文化之旅"), QStringLiteral("怡宝定制饮水机"), QStringLiteral("怡宝定制饮水机"),
QStringLiteral("再来一次"), QStringLiteral("再来一次"), QStringLiteral("谢谢参与"), QStringLiteral("谢谢参与"), QStringLiteral("Iphone手机")
                        };

LotteryDemo::LotteryDemo(QWidget *parent)
    : QWidget(parent)
    , m_rotation(0)
{
    ui.setupUi(this);
    initControl();
}

LotteryDemo::~LotteryDemo()
{

}

void LotteryDemo::initControl()
{
    qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
    setMouseTracking(true);
}

int LotteryDemo::getRotate()
{
    return m_rotation;
}

void LotteryDemo::setRotate(int rotate)
{
    m_rotation = rotate;
    update();
}

void LotteryDemo::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
    painter.save();
    //窗口宽、高
    int nWindowWidth = this->width();
    int nWindowHeight = this->height();
    //背景图
    QPixmap pixmap = QPixmap(":/LotteryDemo/Resources/back.png");
    //中心偏移至中心位置
    painter.translate(nWindowWidth / 2, nWindowHeight / 2);
    //旋转m_rotation角度
    painter.rotate(-1 * m_rotation);
    painter.drawPixmap(QRect(0 - nWindowWidth / 2, 0 - nWindowHeight / 2, pixmap.width(), pixmap.height()), pixmap);
    painter.restore();
    //绘制方向指针
    QPixmap pointer = QPixmap(":/LotteryDemo/Resources/pointer.png");
    m_pointerRect = QRect((nWindowWidth - pointer.width()) / 2, (nWindowHeight - pointer.height() - 30) / 2, pointer.width(), pointer.height());
    painter.drawPixmap(m_pointerRect, pointer);
    __super::paintEvent(event);
}

void LotteryDemo::mousePressEvent(QMouseEvent *event)
{
    if (qApp->mouseButtons() == Qt::LeftButton)
    {
        QRegion ellipseRegion(m_pointerRect, QRegion::Ellipse);
        if (ellipseRegion.contains(event->pos()))
        {
            int rotateRand = qrand() % 360 + 360 * 5;
            m_rotation = 0;
            QPropertyAnimation *animation = new QPropertyAnimation(this, "rotate");
            animation->setEasingCurve(QEasingCurve::OutCubic);
            animation->setDuration(5000);
            animation->setStartValue(0);
            animation->setEndValue(rotateRand);

            connect(animation, SIGNAL(finished()), this, SLOT(onRotateFinished()));
            animation->start(QAbstractAnimation::DeleteWhenStopped);
        }

    }
    __super::mousePressEvent(event);
}

void LotteryDemo::mouseMoveEvent(QMouseEvent *event)
{
    QRegion ellipseRegion(m_pointerRect, QRegion::Ellipse);
    if (ellipseRegion.contains(event->pos()))
    {
        setCursor(Qt::PointingHandCursor);
    }
    else
    {
        setCursor(Qt::ArrowCursor);
    }
    __super::mouseMoveEvent(event);
}

void LotteryDemo::onRotateFinished()
{
    float rotation = m_rotation - 360 * 5;
    int currentIndex = 0;
    for (int index = 0; index < 16; index++)
    {
        rotation -= 22.5;
        if (rotation <= 0)
        {
            currentIndex = index;
            break;
        }
    }

    QMessageBox msgBox;
    QString message = QStringLiteral("恭喜您中奖:%1 !").arg(LottertData[currentIndex]);
    msgBox.setText(message);
    msgBox.exec();
}

结尾

更新精彩Qt作品内容,欢迎关注雨田哥的知乎

版权声明:本文为weixin_42602726原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。