幸运电子转盘
- 基础绘图
- 通过paintEvent来绘图
- 鼠标事件:鼠标左键单击开始旋转
- Timer:定时器
- 信号与槽
1 #ifndef WIDGET_H 2 #define WIDGET_H 3 4 #include <QWidget> 5 #include <QEvent> 6 #include <QDebug> 7 #include <QTimer> 8 #include <QTime> 9 #include <QMouseEvent> 10 #include <QPainter> 11 12 QT_BEGIN_NAMESPACE 13 namespace Ui { class Widget; } 14 QT_END_NAMESPACE 15 16 class Widget : public QWidget 17 { 18 Q_OBJECT 19 20 public: 21 Widget(QWidget *parent = nullptr); 22 ~Widget(); 23 24 protected: 25 // 绘图事件 :显示隐藏 resize update 26 virtual void paintEvent(QPaintEvent *event); 27 virtual void mousePressEvent(QMouseEvent *event); 28 29 30 public slots: 31 void rtTimerOutSlot(); 32 void luckStartSlot(); 33 signals: 34 void luckStartSignal(); 35 private: 36 Ui::Widget *ui; 37 38 private: 39 // 堆区对象 画家 40 QPainter rotationPainter; 41 QPainter pointPainter; 42 43 // 定时器 44 QTimer rtTimer; 45 46 // 角度 47 qreal rtAngle; 48 int randNumber; 49 50 }; 51 #endif // WIDGET_H
1 #include "widget.h" 2 #include "ui_widget.h" 3 4 Widget::Widget(QWidget *parent) 5 : QWidget(parent) 6 , ui(new Ui::Widget) 7 ,rtAngle(0.0) 8 { 9 ui->setupUi(this); 10 11 connect(this,SIGNAL(luckStartSignal()),this, 12 SLOT(luckStartSlot())); 13 14 connect(&rtTimer,SIGNAL(timeout()),this,SLOT(rtTimerOutSlot())); 15 } 16 17 Widget::~Widget() 18 { 19 delete ui; 20 } 21 22 void Widget::paintEvent(QPaintEvent *event) 23 { 24 // 先准备一个画笔 25 rotationPainter.begin(this); 26 // 移动坐标 27 rotationPainter.translate(200,200); 28 // 旋转 29 rotationPainter.rotate(rtAngle); 30 // 设置反锯齿 31 rotationPainter.setRenderHints(QPainter::SmoothPixmapTransform); 32 // 0,0 为POS 33 rotationPainter.drawPixmap(-200,-200,400,400,QPixmap(":/img/1.jpg")); // pos 34 rotationPainter.end(); 35 36 // 绘制指针 37 pointPainter.begin(this); 38 pointPainter.translate(200,200);// 原点移动 39 static const QPoint point[4] = {QPoint(0,18),QPoint(20,0), 40 QPoint(0,-100),QPoint(-20,0)}; 41 // 设置画笔 42 pointPainter.setBrush(QColor(Qt::darkBlue)); 43 // 添加反锯齿(走样) 设置抗锯齿需要再绘图之前添加 44 pointPainter.setRenderHints(QPainter::Antialiasing); 45 // 描点画出多边形 46 pointPainter.drawPolygon(point, 4); 47 48 // 多边形 49 QRect recatanle(-7,-7,14,18); 50 pointPainter.setBrush(QColor(Qt::yellow)); 51 pointPainter.drawEllipse(recatanle); 52 pointPainter.end(); 53 } 54 55 void Widget::rtTimerOutSlot() 56 { 57 rtAngle++; //旋转因子 58 59 if((rtAngle - randNumber) == 90) 60 { 61 rtTimer.setInterval(10); 62 } 63 else if((rtAngle - randNumber) == 180) 64 65 { 66 rtTimer.setInterval(15); 67 } 68 else if((rtAngle - randNumber) == 270) 69 { 70 rtTimer.setInterval(20); 71 } 72 else if((rtAngle - randNumber) == 360) 73 { 74 rtAngle--; //停下来 75 } 76 update(); // 更新视图 77 } 78 79 void Widget::luckStartSlot() 80 { 81 rtAngle = 1; 82 srand(QTime(0,0,0).secsTo(QTime::currentTime())); 83 randNumber = rand()%360 +180;// 180 -> 360之间 84 rtTimer.start(5); 85 } 86 87 void Widget::mousePressEvent(QMouseEvent *event) 88 { 89 if(event->button() == Qt::LeftButton) 90 { 91 if(event->pos().x() >180 && event->pos().x()<220 92 && event->pos().y()<216 && event->pos().y()>130) 93 { 94 emit luckStartSignal(); 95 } 96 } 97 98 }
标签:实战,Widget,pointPainter,Qt,rtAngle,void,include,event,转盘 From: https://www.cnblogs.com/ybqjymy/p/18020899