Qt編寫(xiě)自定義控件實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤(pán)
本文實(shí)例為大家分享了Qt自定義控件實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤(pán)的具體代碼,供大家參考,具體內(nèi)容如下
#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); ?//反走樣開(kāi)啟 ? ? const auto rect = event->rect(); ? ? auto radius = std::min(rect.width(),rect.height()) / 2 - 25; ? ? ? painter.save(); ? ? painter.translate(rect.center()); //將坐標(biāo)系的原點(diǎn)設(shè)置為(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; }
效果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解C++中的指針結(jié)構(gòu)體數(shù)組以及指向結(jié)構(gòu)體變量的指針
這篇文章主要介紹了C++中的指針結(jié)構(gòu)體數(shù)組以及指向結(jié)構(gòu)體變量的指針的用法,是C++入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09C++?線(xiàn)段樹(shù)原理與實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了C++?線(xiàn)段樹(shù)原理與實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09C++中vector容器的常用操作方法實(shí)例總結(jié)
vector容器一般被用作創(chuàng)建動(dòng)態(tài)數(shù)組,動(dòng)態(tài)數(shù)組就像Python中的list結(jié)構(gòu)一樣,可以比普通數(shù)組擁有更豐富操作方法,下面就為大家整理了一些最常用的操作:2016-05-05淺談C++內(nèi)存分配及變長(zhǎng)數(shù)組的動(dòng)態(tài)分配
下面小編就為大家?guī)?lái)一篇淺談C++內(nèi)存分配及變長(zhǎng)數(shù)組的動(dòng)態(tài)分配。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09C++?QT?QThread啟動(dòng)、停止、暫停和恢復(fù)的實(shí)現(xiàn)
本文主要介紹了C++?QT?QThread啟動(dòng)、停止、暫停和恢復(fù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06C++雙線(xiàn)程調(diào)用網(wǎng)絡(luò)攝像頭與多線(xiàn)程調(diào)用多攝像頭同步執(zhí)行方法詳細(xì)講解
這篇文章主要介紹了C++雙線(xiàn)程調(diào)用網(wǎng)絡(luò)攝像頭與多線(xiàn)程調(diào)用多攝像頭同步執(zhí)行方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-11-11C語(yǔ)言學(xué)習(xí)之指針知識(shí)總結(jié)
想突破C語(yǔ)言的學(xué)習(xí),對(duì)指針的掌握是非常重要的,本文為大家總結(jié)了C語(yǔ)言中指針的相關(guān)知識(shí)點(diǎn),文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下2022-07-07解析如何在C語(yǔ)言中調(diào)用shell命令的實(shí)現(xiàn)方法
本篇文章是對(duì)如何在C語(yǔ)言中調(diào)用shell命令的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C語(yǔ)言利用UDP實(shí)現(xiàn)群聊聊天室的示例代碼
UDP是一個(gè)輕量級(jí)、不可靠、面向數(shù)據(jù)報(bào)的、無(wú)連接的傳輸層協(xié)議,多用于可靠性要求不嚴(yán)格,不是非常重要的傳輸,如直播、視頻會(huì)議等等。本文將利用UDP實(shí)現(xiàn)簡(jiǎn)單的群聊聊天室,感興趣的可以了解一下2022-08-08