<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了Qt自定義控制元件實現抽獎轉盤的具體程式碼,供大家參考,具體內容如下
#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()); //將座標系的原點設定為(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; }
效果:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45