Qt5.9.5 隨機(jī)轉(zhuǎn)盤小項(xiàng)目的實(shí)現(xiàn)示例
前言:
本文簡(jiǎn)述:代碼中有點(diǎn)小bug(已經(jīng)粗暴解決),不提倡所有這類型bug都這樣解決
問(wèn)題代碼段:
問(wèn)題描述:由于超時(shí)信號(hào)造成槽函數(shù)形成了死循環(huán),也沒(méi)啥好的方案去替換。
//問(wèn)題根源 connect(&rtTimer, SIGNAL(timeout()), this, SLOT(rtTimeoutSlot()));//調(diào)用超時(shí)信號(hào),計(jì)時(shí)旋轉(zhuǎn) //死循環(huán)部分 void LuckWard::rtTimeoutSlot(){ ? ? rotationAngle++;//旋轉(zhuǎn)因子 ?? ?if ((rotationAngle - randNumber) == 90)//當(dāng)rotationAngle - randNumber==90° ?? ?{ ?? ??? ?rtTimer.setInterval(10);//減速到10毫秒 ?? ?} ?? ?else if ((rotationAngle - randNumber) == 180)//當(dāng)rotationAngle - randNumber==180° ?? ?{ ?? ??? ?rtTimer.setInterval(15);//減速到15毫秒 ?? ?} ?? ?else if ((rotationAngle - randNumber) == 270)//當(dāng)rotationAngle - randNumber==270° ?? ?{ ?? ??? ?rtTimer.setInterval(20);//減速到20毫秒 ?? ?} ?? ?else if ((rotationAngle - randNumber) == 360)//當(dāng)rotationAngle - randNumber==360° ?? ?{ ?? ??? ?rotationAngle--;//停下來(lái) ?? ??? ?i++;//(PS:由于此處不知道為啥進(jìn)入了死循環(huán),只能加一個(gè)變量進(jìn)行判斷) ?? ??? ?if (i == 1)//判斷是否是第一次 ?? ??? ?{ ?? ??? ??? ?emit luckOverSignal();//利用信號(hào)調(diào)用結(jié)束彈窗 ?? ??? ?} ?? ?} ?? ?update();//更新數(shù)據(jù) }
解決方法代碼段:
解決方法:在死循環(huán)內(nèi)添加了一個(gè)全局函數(shù)進(jìn)行計(jì)數(shù),判斷是否是第一次。從而調(diào)用結(jié)束信號(hào),彈出窗口。
頭文件處添加一個(gè)全局變量:
private: Ui::LuckWardClass ui; int i = 0; //記錄變量
實(shí)現(xiàn)文件處添加一個(gè)“if”進(jìn)行判斷
//void LuckWard::rtTimeoutSlot()處代碼段 else if ((rotationAngle - randNumber) == 360)//當(dāng)rotationAngle - randNumber==360° { rotationAngle--;//停下來(lái) //解決方法: i++;//(PS:由于此處不知道為啥進(jìn)入了死循環(huán),只能加一個(gè)變量進(jìn)行判斷) if (i == 1)//判斷是否是第一次 { emit luckOverSignal();//利用信號(hào)調(diào)用結(jié)束彈窗 } }
源碼:
頭文件:luckward.h
#pragma once #include <QtWidgets/QWidget> #include "ui_luckward.h" #include <QPainter> #include <QDebug> #include <QTimer> #include <QMouseEvent> #include <QTime> #include <QMessageBox> class LuckWard : public QWidget { ?? ?Q_OBJECT public: ?? ?LuckWard(QWidget *parent = Q_NULLPTR); ?? ?//重載繪制事件 ?? ?void paintEvent(QPaintEvent *ev); ?? ?//重載鼠標(biāo)按下事件 ?? ?void mousePressEvent(QMouseEvent *ev); ?? ?public slots: ?? ?//計(jì)時(shí)旋轉(zhuǎn)函數(shù) ?? ?void rtTimeoutSlot(); ?? ?//轉(zhuǎn)盤開(kāi)始旋轉(zhuǎn)函數(shù) ?? ?void luckStartSlot(); ?? ?//轉(zhuǎn)盤結(jié)束旋轉(zhuǎn)函數(shù) ?? ?void luckOverSlot(); signals: ?? ?//轉(zhuǎn)盤開(kāi)始旋轉(zhuǎn)信號(hào) ?? ?void luckStartSignal(); ?? ?//轉(zhuǎn)盤結(jié)束信號(hào) ?? ?void luckOverSignal(); private: ?? ?Ui::LuckWardClass ui; ?? ?int i = 0; ? ? ? ? ? ? ? ? ?//記錄變量? ?? ?QPainter rotationPainter; ? //繪畫(huà)轉(zhuǎn)盤 ?? ?int rotationAngle; ? ? ? ? ?//旋轉(zhuǎn)角度 ?? ?int randNumber; ? ? ? ? ? ? //隨機(jī)數(shù) ?? ?int EndNumber; ? ? ? ? ? ? ?//結(jié)束數(shù)值 ?? ?QTimer rtTimer; ? ? ? ? ? ? //旋轉(zhuǎn)速度 ?? ?QPainter pointPainter; ? ? ?//繪畫(huà)箭頭與釘子 };
UI文件:
/******************************************************************************** ** Form generated from reading UI file 'luckward.ui' ** ** Created by: Qt User Interface Compiler version 5.9.5 ** ** WARNING! All changes made in this file will be lost when recompiling UI file! ********************************************************************************/ #ifndef UI_LUCKWARD_H #define UI_LUCKWARD_H #include <QtCore/QVariant> #include <QtWidgets/QAction> #include <QtWidgets/QApplication> #include <QtWidgets/QButtonGroup> #include <QtWidgets/QHeaderView> #include <QtWidgets/QWidget> QT_BEGIN_NAMESPACE class Ui_LuckWardClass { public: ? ? void setupUi(QWidget *LuckWardClass) ? ? { ? ? ? ? if (LuckWardClass->objectName().isEmpty()) ? ? ? ? ? ? LuckWardClass->setObjectName(QStringLiteral("LuckWardClass")); ? ? ? ? LuckWardClass->resize(400, 400); ? ? ? ? LuckWardClass->setMinimumSize(QSize(400, 400)); ? ? ? ? LuckWardClass->setMaximumSize(QSize(400, 400)); ? ? ? ? retranslateUi(LuckWardClass); ? ? ? ? QMetaObject::connectSlotsByName(LuckWardClass); ? ? } // setupUi ? ? void retranslateUi(QWidget *LuckWardClass) ? ? { ? ? ? ? LuckWardClass->setWindowTitle(QApplication::translate("LuckWardClass", "LuckWard", Q_NULLPTR)); ? ? } // retranslateUi }; namespace Ui { ? ? class LuckWardClass: public Ui_LuckWardClass {}; } // namespace Ui QT_END_NAMESPACE #endif // UI_LUCKWARD_H
主函數(shù):main.cpp
#include "luckward.h" #include <QtWidgets/QApplication> int main(int argc, char *argv[]) { ?? ?QApplication a(argc, argv); ?? ?LuckWard w; ?? ?w.show(); ?? ?return a.exec(); }
實(shí)現(xiàn)文件:luckward.cpp
#include "luckward.h" LuckWard::LuckWard(QWidget *parent) ?? ?: QWidget(parent) { ?? ?ui.setupUi(this); ?? ?connect(&rtTimer, SIGNAL(timeout()), this, SLOT(rtTimeoutSlot()));//調(diào)用超時(shí)信號(hào),計(jì)時(shí)旋轉(zhuǎn) ?? ?connect(this, SIGNAL(luckStartSignal()), this, SLOT(luckStartSlot()));//用luckStartSignal()發(fā)出的信號(hào)使轉(zhuǎn)盤開(kāi)始旋轉(zhuǎn) ?? ?connect(this, SIGNAL(luckOverSignal()), this, SLOT(luckOverSlot()));//用luckOverSignal()發(fā)出的信號(hào)使轉(zhuǎn)盤結(jié)束后彈窗 } void LuckWard::paintEvent(QPaintEvent *ev) { ?? ?//繪制轉(zhuǎn)盤 ?? ?rotationPainter.begin(this);//開(kāi)始繪畫(huà) ?? ?rotationPainter.setRenderHint(QPainter::SmoothPixmapTransform);//抗鋸齒化 ?? ?rotationPainter.translate(200, 200);//修改圖片中心點(diǎn) ?? ?rotationPainter.rotate(rotationAngle);//使圖片旋轉(zhuǎn)30° ?? ?rotationPainter.drawPixmap(-200, -200, 400, 400, QPixmap("luck.png"));//添加圖片 ?? ?rotationPainter.end();//結(jié)束繪畫(huà) ?? ?//繪制箭頭 ?? ?pointPainter.begin(this);//開(kāi)始繪畫(huà) ?? ?pointPainter.setRenderHint(QPainter::SmoothPixmapTransform); ?? ?pointPainter.translate(200, 200);//設(shè)置繪制坐標(biāo)位置 ?? ?static const QPoint point[4] = { QPoint(0, 18), QPoint(20, 0), QPoint(0, -100), QPoint(-20, 0) };//繪制路徑 ?? ?pointPainter.setBrush(QColor(Qt::blue));//設(shè)置箭頭顏色 ?? ?pointPainter.drawPolygon(point, 4); ?? ?//繪制釘子(處于中間點(diǎn)) ?? ?QRect rectanle(-7, -7, 14, 18); //設(shè)置繪制坐標(biāo)位置 ?? ?pointPainter.setBrush(QColor(Qt::yellow));//設(shè)置釘子顏色 ?? ?pointPainter.drawEllipse(rectanle);//繪制橢圓形 ?? ?pointPainter.end();//結(jié)束繪畫(huà) } void LuckWard::rtTimeoutSlot() { ?? ?rotationAngle++;//旋轉(zhuǎn)因子 ?? ?if ((rotationAngle - randNumber) == 90)//當(dāng)rotationAngle - randNumber==90° ?? ?{ ?? ??? ?rtTimer.setInterval(10);//減速到10毫秒 ?? ?} ?? ?else if ((rotationAngle - randNumber) == 180)//當(dāng)rotationAngle - randNumber==180° ?? ?{ ?? ??? ?rtTimer.setInterval(15);//減速到15毫秒 ?? ?} ?? ?else if ((rotationAngle - randNumber) == 270)//當(dāng)rotationAngle - randNumber==270° ?? ?{ ?? ??? ?rtTimer.setInterval(20);//減速到20毫秒 ?? ?} ?? ?else if ((rotationAngle - randNumber) == 360)//當(dāng)rotationAngle - randNumber==360° ?? ?{ ?? ??? ?rotationAngle--;//停下來(lái) ?? ??? ?i++;//(PS:由于此處不知道為啥進(jìn)入了死循環(huán),只能加一個(gè)變量進(jìn)行判斷) ?? ??? ?if (i == 1)//判斷是否是第一次 ?? ??? ?{ ?? ??? ??? ?emit luckOverSignal();//利用信號(hào)調(diào)用結(jié)束彈窗 ?? ??? ?} ?? ?} ?? ?update();//更新數(shù)據(jù) } void LuckWard::mousePressEvent(QMouseEvent *ev) { ?? ?if (ev->button() == Qt::LeftButton)//判斷是否鼠標(biāo)左鍵按下 ?? ?{ ?? ??? ?qDebug() << "LeftButton Press" << ev->pos();//打印按下的位置 ?? ??? ?//判斷是否在此范圍內(nèi)按下鼠標(biāo)左鍵(x(180,220),y(216,130)) ?? ??? ?if (ev->pos().x() > 180 && ev->pos().x() < 220 && ev->pos().y()<216 && ev->pos().y()>130) ?? ??? ?{ ?? ??? ??? ?emit luckStartSignal(); ?? ??? ?} ?? ?} } void LuckWard::luckStartSlot() { ?? ?rtTimer.setInterval(50);//設(shè)置旋轉(zhuǎn)速度為50毫秒 ?? ?rotationAngle = 1;//初始化旋轉(zhuǎn)角為1 ?? ?qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));//初始化隨機(jī)數(shù) ?? ?randNumber = qrand() % 360 + 180; ?//設(shè)置隨機(jī)數(shù)取值180 - >360之間 ?? ?rtTimer.start(1);//定時(shí)器開(kāi)始 } void LuckWard::luckOverSlot() { ?? ?qDebug() << "rotationAngle The Angle" << EndNumber; ?? ?if (EndNumber>0) ?? ?{ ?? ??? ?QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("恭喜您!中獎(jiǎng)了"), QStringLiteral("確定")); ?? ?} }
ui界面布局樣式:
效果圖
到此這篇關(guān)于Qt 5.9.5 隨機(jī)轉(zhuǎn)盤小項(xiàng)目的文章就介紹到這了,更多相關(guān)Qt 5.9.5 隨機(jī)轉(zhuǎn)盤小項(xiàng)目?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言詳細(xì)講解循環(huán)語(yǔ)句的妙用
C語(yǔ)言循環(huán)控制語(yǔ)句是一個(gè)基于C語(yǔ)言的編程語(yǔ)句,該語(yǔ)句主要有while循環(huán)語(yǔ)句、do-while循環(huán)語(yǔ)句和for循環(huán)語(yǔ)句來(lái)實(shí)現(xiàn)循環(huán)結(jié)構(gòu),在循環(huán)過(guò)程中還有關(guān)鍵字break、continue、do、break控制中斷繼續(xù)與結(jié)束等操作2022-04-04基于C++實(shí)現(xiàn)高精度計(jì)時(shí)器
chrono是C++ 11中的時(shí)間庫(kù),它提供了跨平臺(tái)的高精度時(shí)鐘解決方案,精確到納秒級(jí),本文主要為大家詳細(xì)介紹了如何使用chrono實(shí)現(xiàn)高精度計(jì)時(shí)器,感興趣的可以了解下2024-02-02C語(yǔ)言超詳細(xì)講解getchar函數(shù)的使用
C 庫(kù)函數(shù) int getchar(void) 從標(biāo)準(zhǔn)輸入 stdin 獲取一個(gè)字符(一個(gè)無(wú)符號(hào)字符)。這等同于 getc 帶有 stdin 作為參數(shù),下面讓我們?cè)敿?xì)來(lái)看看2022-05-05C語(yǔ)言實(shí)現(xiàn)楊輝三角實(shí)例
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)楊輝三角的方法,主要通過(guò)數(shù)組簡(jiǎn)單實(shí)現(xiàn),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-09-09C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的通訊錄
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的通訊錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02linux c 獲得當(dāng)前進(jìn)程的進(jìn)程名和執(zhí)行路徑(示例)
如何得到當(dāng)前進(jìn)程的進(jìn)程名和執(zhí)行路徑。寫(xiě)了個(gè)程序分享一下2013-07-07