Qt 5.9.5 随机转盘小项目

打印 上一主题 下一主题

主题 507|帖子 507|积分 1521

随机转盘小项目



前言:

   本文简述:代码中有点小bug(已经粗暴解决),不提倡所有这类型bug都这样解决
  问题代码段:

问题描述:由于超时信号造成槽函数形成了死循环,也没啥好的方案去替换。
  1. //问题根源
  2. connect(&rtTimer, SIGNAL(timeout()), this, SLOT(rtTimeoutSlot()));//调用超时信号,计时旋转
  3. //死循环部分
  4. void LuckWard::rtTimeoutSlot(){
  5.     rotationAngle++;//旋转因子
  6.         if ((rotationAngle - randNumber) == 90)//当rotationAngle - randNumber==90°
  7.         {
  8.                 rtTimer.setInterval(10);//减速到10毫秒
  9.         }
  10.         else if ((rotationAngle - randNumber) == 180)//当rotationAngle - randNumber==180°
  11.         {
  12.                 rtTimer.setInterval(15);//减速到15毫秒
  13.         }
  14.         else if ((rotationAngle - randNumber) == 270)//当rotationAngle - randNumber==270°
  15.         {
  16.                 rtTimer.setInterval(20);//减速到20毫秒
  17.         }
  18.         else if ((rotationAngle - randNumber) == 360)//当rotationAngle - randNumber==360°
  19.         {
  20.                 rotationAngle--;//停下来
  21.                 i++;//(PS:由于此处不知道为啥进入了死循环,只能加一个变量进行判断)
  22.                 if (i == 1)//判断是否是第一次
  23.                 {
  24.                         emit luckOverSignal();//利用信号调用结束弹窗
  25.                 }
  26.         }
  27.         update();//更新数据
  28. }
复制代码
解决方法代码段:

解决方法:在死循环内添加了一个全局函数进行计数,判断是否是第一次。从而调用结束信号,弹出窗口。
头文件处添加一个全局变量:
  1. private:
  2.         Ui::LuckWardClass ui;
  3.         int i = 0;                  //记录变量
复制代码
实现文件处添加一个“if”进行判断
  1.     //void LuckWard::rtTimeoutSlot()处代码段
  2.         else if ((rotationAngle - randNumber) == 360)//当rotationAngle - randNumber==360°
  3.         {
  4.                 rotationAngle--;//停下来
  5.         //解决方法:
  6.                 i++;//(PS:由于此处不知道为啥进入了死循环,只能加一个变量进行判断)
  7.                 if (i == 1)//判断是否是第一次
  8.                 {
  9.                         emit luckOverSignal();//利用信号调用结束弹窗
  10.                 }
  11.         }
复制代码
源码:

头文件:luckward.h

  1. #pragma once#include #include "ui_luckward.h"#include #include #include #include #include #include class LuckWard : public QWidget{        Q_OBJECTpublic:        LuckWard(QWidget *parent = Q_NULLPTR);        //重载绘制事件        void paintEvent(QPaintEvent *ev);        //重载鼠标按下事件        void mousePressEvent(QMouseEvent *ev);        public slots:        //计时旋转函数        void rtTimeoutSlot();        //转盘开始旋转函数        void luckStartSlot();        //转盘结束旋转函数        void luckOverSlot();signals:        //转盘开始旋转信号        void luckStartSignal();        //转盘结束信号        void luckOverSignal();private:
  2.         Ui::LuckWardClass ui;
  3.         int i = 0;                  //记录变量         QPainter rotationPainter;   //绘画转盘        int rotationAngle;          //旋转角度        int randNumber;             //随机数        int EndNumber;              //结束数值        QTimer rtTimer;             //旋转速度        QPainter pointPainter;      //绘画箭头与钉子};
复制代码
UI文件:

  1. /********************************************************************************
  2. ** Form generated from reading UI file 'luckward.ui'
  3. **
  4. ** Created by: Qt User Interface Compiler version 5.9.5
  5. **
  6. ** WARNING! All changes made in this file will be lost when recompiling UI file!
  7. ********************************************************************************/
  8. #ifndef UI_LUCKWARD_H
  9. #define UI_LUCKWARD_H
  10. #include <QtCore/QVariant>
  11. #include <QtWidgets/QAction>
  12. #include <QtWidgets/QApplication>
  13. #include <QtWidgets/QButtonGroup>
  14. #include <QtWidgets/QHeaderView>
  15. #include <QtWidgets/QWidget>
  16. QT_BEGIN_NAMESPACE
  17. class Ui_LuckWardClass
  18. {
  19. public:
  20.     void setupUi(QWidget *LuckWardClass)
  21.     {
  22.         if (LuckWardClass->objectName().isEmpty())
  23.             LuckWardClass->setObjectName(QStringLiteral("LuckWardClass"));
  24.         LuckWardClass->resize(400, 400);
  25.         LuckWardClass->setMinimumSize(QSize(400, 400));
  26.         LuckWardClass->setMaximumSize(QSize(400, 400));
  27.         retranslateUi(LuckWardClass);
  28.         QMetaObject::connectSlotsByName(LuckWardClass);
  29.     } // setupUi
  30.     void retranslateUi(QWidget *LuckWardClass)
  31.     {
  32.         LuckWardClass->setWindowTitle(QApplication::translate("LuckWardClass", "LuckWard", Q_NULLPTR));
  33.     } // retranslateUi
  34. };
  35. namespace Ui {
  36.     class LuckWardClass: public Ui_LuckWardClass {};
  37. } // namespace Ui
  38. QT_END_NAMESPACE
  39. #endif // UI_LUCKWARD_H
复制代码
主函数:main.cpp

  1. #include "luckward.h"
  2. #include <QtWidgets/QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5.         QApplication a(argc, argv);
  6.         LuckWard w;
  7.         w.show();
  8.         return a.exec();
  9. }
复制代码
实现文件:luckward.cpp

[code]#include "luckward.h"LuckWard::LuckWard(QWidget *parent)        : QWidget(parent){        ui.setupUi(this);        connect(&rtTimer, SIGNAL(timeout()), this, SLOT(rtTimeoutSlot()));//调用超时信号,计时旋转        connect(this, SIGNAL(luckStartSignal()), this, SLOT(luckStartSlot()));//用luckStartSignal()发出的信号使转盘开始旋转        connect(this, SIGNAL(luckOverSignal()), this, SLOT(luckOverSlot()));//用luckOverSignal()发出的信号使转盘结束后弹窗}void LuckWard::paintEvent(QPaintEvent *ev){        //绘制转盘        rotationPainter.begin(this);//开始绘画        rotationPainter.setRenderHint(QPainter::SmoothPixmapTransform);//抗锯齿化        rotationPainter.translate(200, 200);//修改图片中心点        rotationPainter.rotate(rotationAngle);//使图片旋转30°        rotationPainter.drawPixmap(-200, -200, 400, 400, QPixmap("luck.png"));//添加图片        rotationPainter.end();//结束绘画        //绘制箭头        pointPainter.begin(this);//开始绘画        pointPainter.setRenderHint(QPainter::SmoothPixmapTransform);        pointPainter.translate(200, 200);//设置绘制坐标位置        static 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);        //绘制钉子(处于中间点)        QRect rectanle(-7, -7, 14, 18); //设置绘制坐标位置        pointPainter.setBrush(QColor(Qt::yellow));//设置钉子颜色        pointPainter.drawEllipse(rectanle);//绘制椭圆形        pointPainter.end();//结束绘画}void LuckWard::rtTimeoutSlot(){        rotationAngle++;//旋转因子        if ((rotationAngle - randNumber) == 90)//当rotationAngle - randNumber==90°        {                rtTimer.setInterval(10);//减速到10毫秒        }        else if ((rotationAngle - randNumber) == 180)//当rotationAngle - randNumber==180°        {                rtTimer.setInterval(15);//减速到15毫秒        }        else if ((rotationAngle - randNumber) == 270)//当rotationAngle - randNumber==270°        {                rtTimer.setInterval(20);//减速到20毫秒        }        else if ((rotationAngle - randNumber) == 360)//当rotationAngle - randNumber==360°        {                rotationAngle--;//停下来                i++;//(PS:由于此处不知道为啥进入了死循环,只能加一个变量进行判断)                if (i == 1)//判断是否是第一次                {                        emit luckOverSignal();//利用信号调用结束弹窗                }        }        update();//更新数据}void LuckWard::mousePressEvent(QMouseEvent *ev){        if (ev->button() == Qt::LeftButton)//判断是否鼠标左键按下        {                qDebug() pos().x() > 180 && ev->pos().x() < 220 && ev->pos().y()pos().y()>130)                {                        emit luckStartSignal();                }        }}void LuckWard::luckStartSlot(){        rtTimer.setInterval(50);//设置旋转速度为50毫秒        rotationAngle = 1;//初始化旋转角为1        qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));//初始化随机数        randNumber = qrand() % 360 + 180;  //设置随机数取值180 - >360之间        rtTimer.start(1);//定时器开始}void LuckWard::luckOverSlot(){        qDebug()
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

徐锦洪

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表