抽奖
一、新建项目
打开ui文件设计界面

<?xml version="1.0" encoding="UTF-8"?><ui version="4.0"><class>ErnieDialog</class><widget class="QDialog" name="ErnieDialog"><property name="geometry"><rect><x>0</x><y>0</y><width>420</width><height>600</height></rect></property><property name="windowTitle"><string>抽奖</string></property><layout class="QVBoxLayout" name="verticalLayout"><item><widget class="QFrame" name="frame"><property name="sizePolicy"><sizepolicy hsizetype="Preferred" vsizetype="Expanding"><horstretch>0</horstretch><verstretch>0</verstretch></sizepolicy></property><property name="frameShape"><enum>QFrame::WinPanel</enum></property><property name="frameShadow"><enum>QFrame::Plain</enum></property><property name="lineWidth"><number>5</number></property><property name="midLineWidth"><number>3</number></property></widget></item><item><layout class="QHBoxLayout" name="horizontalLayout"><item><spacer name="horizontalSpacer"><property name="orientation"><enum>Qt::Horizontal</enum></property><property name="sizeHint" stdset="0"><size><width>40</width><height>20</height></size></property></spacer></item><item><widget class="QPushButton" name="pushButton"><property name="font"><font><family>楷体</family><pointsize>20</pointsize></font></property><property name="text"><string>开始</string></property></widget></item><item><spacer name="horizontalSpacer_2"><property name="orientation"><enum>Qt::Horizontal</enum></property><property name="sizeHint" stdset="0"><size><width>40</width><height>20</height></size></property></spacer></item></layout></item></layout></widget><layoutdefault spacing="6" margin="11"/><resources/><connections/></ui>
二、项目代码
首先、更改项目构建设置,把图片资源文件复制到项目目录中

然后开始编写头文件erniedialog.h
#ifndef ERNIEDIALOG_H#define ERNIEDIALOG_H#include <QDialog>#include <QDebug>#include <QPainter>#include <QDir>#include <QTime>#include <QVector>//向量容器namespace Ui {class ErnieDialog;}class ErnieDialog : public QDialog{Q_OBJECTpublic:explicit ErnieDialog(QWidget *parent = 0);~ErnieDialog();private slots://开始按钮对应的槽函数void on_pushButton_clicked();private://绘图事件处理器void paintEvent(QPaintEvent *);//定时器事件处理函数void timerEvent(QTimerEvent *);//加载指定目录下的图片void loadPhotos(const QString& path);private:Ui::ErnieDialog *ui;//保存所有图片的容器QVector<QImage> m_vecPhotos;//容器的索引int m_index;//切换图片的定时器int m_timer;//状态的标记(true开始/false停止摇奖)bool isStarted;};#endif // ERNIEDIALOG_H
最后编辑erniedialog.cpp
#include "erniedialog.h"#include "ui_erniedialog.h"ErnieDialog::ErnieDialog(QWidget *parent) :QDialog(parent),ui(new Ui::ErnieDialog){ui->setupUi(this);//初始化状态标记isStarted = false;//设置随机种子(毫秒值)qsrand(QTime::currentTime().msec());//初始化索引m_index = 0;//加载./photos目录下的图片到容器(photos目录拷贝到工程目录下)loadPhotos("./photos");qDebug() << "加载出" << m_vecPhotos.size() << "个图片";}ErnieDialog::~ErnieDialog(){delete ui;}//开始按钮对应的槽函数void ErnieDialog::on_pushButton_clicked(){if(isStarted == false){isStarted = true;//开始摇奖//每隔50毫秒出发一个定时器事件m_timer = startTimer(50);//按钮文本改成停止ui->pushButton->setText("停止");}else{isStarted = false;//停止摇奖//关闭定时器killTimer(m_timer);//按钮文本改成开始ui->pushButton->setText("开始");}}void ErnieDialog::paintEvent(QPaintEvent *){//创建画家对象QPainter painter(this);//获取要绘制图片所在的矩形区QRect rcPhoto = ui->frame->frameRect();//坐标平移,让frame对象和坐标一致rcPhoto.translate(ui->frame->pos());//绘图painter.drawImage(rcPhoto,m_vecPhotos[m_index]);}void ErnieDialog::timerEvent(QTimerEvent *){//随机获取一个图片的索引m_index = qrand()%m_vecPhotos.size();//出发绘图事件,将随机获取的图片显示到frameupdate();}void ErnieDialog::loadPhotos(const QString& path){//创建一个目录对象QDir dir(path);//遍历当前目录的所有图片(过滤方式:文件)foreach (QString file, dir.entryList(QDir::Files)) {//根据文件名创建图片对象"./photos/x.x"QImage image(path+"/"+file);//保存图片到容器m_vecPhotos << image;}//递归遍历子目录(过滤方式:目录)foreach (QString subDir, dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot)) {loadPhotos(path+"/"+subDir);}}
