Qt編寫自定義控件實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤
本文實(shí)例為大家分享了Qt自定義控件實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎ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); ?//反走樣開啟
? ? 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;
}效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
OpenCV4.1.0+VisualStudio2019開發(fā)環(huán)境搭建(超級簡單)
這篇文章主要介紹了OpenCV4.1.0+VisualStudio2019開發(fā)環(huán)境搭建(超級簡單),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
C++中智能指針最常用的shared_ptr和unique_ptr
C++中的智能指針最常用的是shared_ptr和unique_ptr,C++新手最常問的問題是我從一個(gè)函數(shù)中拿到unique_ptr,但要轉(zhuǎn)成shared_ptr才能使用,要怎么轉(zhuǎn)換?同理是否能將shared_ptr轉(zhuǎn)換成unique_ptr,面對這些問題,跟隨小編一起看看吧2022-08-08
C語言中函數(shù)棧幀的創(chuàng)建和銷毀的深層分析
在C語言中,每一個(gè)正在運(yùn)行的函數(shù)都有一個(gè)棧幀與其對應(yīng),棧幀中存儲(chǔ)的是該函數(shù)的返回地址和局部變量。從邏輯上講,棧幀就是一個(gè)函數(shù)執(zhí)行的環(huán)境:函數(shù)參數(shù)、函數(shù)的局部變量、函數(shù)執(zhí)行完后返回到哪里等等2022-04-04
C#委托所蘊(yùn)含的函數(shù)指針概念詳細(xì)解析
C#中用委托這種概念實(shí)現(xiàn)了函數(shù)指針技術(shù)而已,另外.ent提供額外的安全性,當(dāng)然也損失了靈活性2013-09-09
Qt實(shí)現(xiàn)圖片移動(dòng)實(shí)例(圖文教程)
這學(xué)期實(shí)訓(xùn)的時(shí)候用MFC做過一個(gè)飛機(jī)大戰(zhàn),很無聊的東西,一直想用Qt做一個(gè);首先需要解決的問題是圖片的移動(dòng),怎么說飛機(jī)啊子彈啊都是動(dòng)著的,圖片當(dāng)然要跑起來,感興趣的你可不要走開啊2013-01-01
C語言實(shí)現(xiàn)動(dòng)態(tài)順序表的實(shí)現(xiàn)代碼
這篇文章主要介紹了C語言實(shí)現(xiàn)動(dòng)態(tài)順序表的實(shí)現(xiàn)代碼的相關(guān)資料,動(dòng)態(tài)順序表在內(nèi)存中開辟一塊空間,可以隨我們數(shù)據(jù)數(shù)量的增多來擴(kuò)容,需要的朋友可以參考下2017-08-08
基于c++的中國象棋游戲設(shè)計(jì)與實(shí)現(xiàn)
這篇文章主要介紹了基于c++的中國象棋游戲設(shè)計(jì)與實(shí)現(xiàn),主要操作是possibleMove(int?x,?int?y),通過整個(gè)棋盤每個(gè)位置上的信息、中國象棋的規(guī)則來獲得位置(x,?y)這個(gè)棋子可以移動(dòng)到的位置,需要的朋友可以參考一下2022-02-02
使用UART與PC通信實(shí)現(xiàn)msp430g2553單片機(jī)超聲波測距示例
這篇文章主要介紹了使用UART與PC通信實(shí)現(xiàn)msp430g2553單片機(jī)超聲波測距示例,需要的朋友可以參考下2014-05-05

