抽奖

一、新建项目

打开ui文件设计界面

image-20210208123522658.png

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>ErnieDialog</class>
  4. <widget class="QDialog" name="ErnieDialog">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>420</width>
  10. <height>600</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>抽奖</string>
  15. </property>
  16. <layout class="QVBoxLayout" name="verticalLayout">
  17. <item>
  18. <widget class="QFrame" name="frame">
  19. <property name="sizePolicy">
  20. <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
  21. <horstretch>0</horstretch>
  22. <verstretch>0</verstretch>
  23. </sizepolicy>
  24. </property>
  25. <property name="frameShape">
  26. <enum>QFrame::WinPanel</enum>
  27. </property>
  28. <property name="frameShadow">
  29. <enum>QFrame::Plain</enum>
  30. </property>
  31. <property name="lineWidth">
  32. <number>5</number>
  33. </property>
  34. <property name="midLineWidth">
  35. <number>3</number>
  36. </property>
  37. </widget>
  38. </item>
  39. <item>
  40. <layout class="QHBoxLayout" name="horizontalLayout">
  41. <item>
  42. <spacer name="horizontalSpacer">
  43. <property name="orientation">
  44. <enum>Qt::Horizontal</enum>
  45. </property>
  46. <property name="sizeHint" stdset="0">
  47. <size>
  48. <width>40</width>
  49. <height>20</height>
  50. </size>
  51. </property>
  52. </spacer>
  53. </item>
  54. <item>
  55. <widget class="QPushButton" name="pushButton">
  56. <property name="font">
  57. <font>
  58. <family>楷体</family>
  59. <pointsize>20</pointsize>
  60. </font>
  61. </property>
  62. <property name="text">
  63. <string>开始</string>
  64. </property>
  65. </widget>
  66. </item>
  67. <item>
  68. <spacer name="horizontalSpacer_2">
  69. <property name="orientation">
  70. <enum>Qt::Horizontal</enum>
  71. </property>
  72. <property name="sizeHint" stdset="0">
  73. <size>
  74. <width>40</width>
  75. <height>20</height>
  76. </size>
  77. </property>
  78. </spacer>
  79. </item>
  80. </layout>
  81. </item>
  82. </layout>
  83. </widget>
  84. <layoutdefault spacing="6" margin="11"/>
  85. <resources/>
  86. <connections/>
  87. </ui>

二、项目代码

首先、更改项目构建设置,把图片资源文件复制到项目目录中

image-20210208124047028.png

然后开始编写头文件erniedialog.h

  1. #ifndef ERNIEDIALOG_H
  2. #define ERNIEDIALOG_H
  3. #include <QDialog>
  4. #include <QDebug>
  5. #include <QPainter>
  6. #include <QDir>
  7. #include <QTime>
  8. #include <QVector>//向量容器
  9. namespace Ui {
  10. class ErnieDialog;
  11. }
  12. class ErnieDialog : public QDialog
  13. {
  14. Q_OBJECT
  15. public:
  16. explicit ErnieDialog(QWidget *parent = 0);
  17. ~ErnieDialog();
  18. private slots:
  19. //开始按钮对应的槽函数
  20. void on_pushButton_clicked();
  21. private:
  22. //绘图事件处理器
  23. void paintEvent(QPaintEvent *);
  24. //定时器事件处理函数
  25. void timerEvent(QTimerEvent *);
  26. //加载指定目录下的图片
  27. void loadPhotos(const QString& path);
  28. private:
  29. Ui::ErnieDialog *ui;
  30. //保存所有图片的容器
  31. QVector<QImage> m_vecPhotos;
  32. //容器的索引
  33. int m_index;
  34. //切换图片的定时器
  35. int m_timer;
  36. //状态的标记(true开始/false停止摇奖)
  37. bool isStarted;
  38. };
  39. #endif // ERNIEDIALOG_H

最后编辑erniedialog.cpp

  1. #include "erniedialog.h"
  2. #include "ui_erniedialog.h"
  3. ErnieDialog::ErnieDialog(QWidget *parent) :
  4. QDialog(parent),
  5. ui(new Ui::ErnieDialog)
  6. {
  7. ui->setupUi(this);
  8. //初始化状态标记
  9. isStarted = false;
  10. //设置随机种子(毫秒值)
  11. qsrand(QTime::currentTime().msec());
  12. //初始化索引
  13. m_index = 0;
  14. //加载./photos目录下的图片到容器(photos目录拷贝到工程目录下)
  15. loadPhotos("./photos");
  16. qDebug() << "加载出" << m_vecPhotos.size() << "个图片";
  17. }
  18. ErnieDialog::~ErnieDialog()
  19. {
  20. delete ui;
  21. }
  22. //开始按钮对应的槽函数
  23. void ErnieDialog::on_pushButton_clicked()
  24. {
  25. if(isStarted == false){
  26. isStarted = true;//开始摇奖
  27. //每隔50毫秒出发一个定时器事件
  28. m_timer = startTimer(50);
  29. //按钮文本改成停止
  30. ui->pushButton->setText("停止");
  31. }
  32. else{
  33. isStarted = false;//停止摇奖
  34. //关闭定时器
  35. killTimer(m_timer);
  36. //按钮文本改成开始
  37. ui->pushButton->setText("开始");
  38. }
  39. }
  40. void ErnieDialog::paintEvent(QPaintEvent *)
  41. {
  42. //创建画家对象
  43. QPainter painter(this);
  44. //获取要绘制图片所在的矩形区
  45. QRect rcPhoto = ui->frame->frameRect();
  46. //坐标平移,让frame对象和坐标一致
  47. rcPhoto.translate(ui->frame->pos());
  48. //绘图
  49. painter.drawImage(rcPhoto,m_vecPhotos[m_index]);
  50. }
  51. void ErnieDialog::timerEvent(QTimerEvent *)
  52. {
  53. //随机获取一个图片的索引
  54. m_index = qrand()%m_vecPhotos.size();
  55. //出发绘图事件,将随机获取的图片显示到frame
  56. update();
  57. }
  58. void ErnieDialog::loadPhotos(const QString& path)
  59. {
  60. //创建一个目录对象
  61. QDir dir(path);
  62. //遍历当前目录的所有图片(过滤方式:文件)
  63. foreach (QString file, dir.entryList(QDir::Files)) {
  64. //根据文件名创建图片对象"./photos/x.x"
  65. QImage image(path+"/"+file);
  66. //保存图片到容器
  67. m_vecPhotos << image;
  68. }
  69. //递归遍历子目录(过滤方式:目录)
  70. foreach (QString subDir, dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot)) {
  71. loadPhotos(path+"/"+subDir);
  72. }
  73. }

photos.zipErnie.zip