基于Qt的抽奖小程序

一:实现思路

Qt绘制抽奖界面

使用一个定时器来实现滚动显示

使用鼠标事件(使得小程序框能够被拖动)

二:项目演示

抽奖小程序

二:源代码

widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include
#include
#include
#include
#define TIMEOUT 20
#define TITLE_MOVE_HIGHT 200
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    void paintEvent(QPaintEvent *e);
    void timerEvent(QTimerEvent *e);
    void mouseMoveEvent(QMouseEvent *e);
    void mousePressEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent *e);
    ~Widget();

private slots:
    void on_BStartButton_clicked();

    void on_CloseButton_clicked();

private:
    bool m_dragging;//判断鼠标是否正在被按下
    QPoint m_startPosition;
    QPoint m_framePosition;

private:
    Ui::Widget *ui;
    QPixmap m_pix;//背景图
    bool m_bDrawing;//判断是否正在抽奖
    int m_MyTimeId;
    QStringList m_listNum;//用来存放中奖者名单
    int m_Curpos;
};

#endif // WIDGET_H

widget.cpp:

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->listWidget->setStyleSheet("background:gray");

    //不显示标题栏(无边框处理)
    this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    setWindowIcon(QIcon("D:\\linux\\pic\\5.jpg"));
    ui->BStartButton->setIcon(QIcon("D:\\linux\\pic\\22.png"));
    ui->BStartButton->setIconSize(QSize(100,100));
    ui->CloseButton->setStyleSheet("QPushButton{border-image:url(D:/linux/pic/31x.jpg);}"
                                   "QPushButton:hover{border-image:url(D:/linux/pic/32x.jpg);}"
                                   "QPushButton:pressed{border-image:url(D:/linux/pic/33x.jpg);}");


    m_pix.load("D:/linux/pic/25.jpg");

    m_bDrawing=false;
    m_Curpos=0;
    m_listNum.push_back("88元立减金");
    m_listNum.push_back("50元立减金");
    m_listNum.push_back("30元立减金");
    m_listNum.push_back("20元立减金");
    m_listNum.push_back("8.88立减金");
    m_listNum.push_back("6.66立减金");
    m_listNum.push_back("5元立减金");
    m_listNum.push_back("美团十元劵");
    m_listNum.push_back("美团5元劵");
    m_listNum.push_back("谢谢参与");
}

void Widget::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
    QRect rc=rect();
    painter.drawPixmap(rc,m_pix);
}

void Widget::timerEvent(QTimerEvent *e)
{
    if(m_MyTimeId==e->timerId())
    {
        m_Curpos++;
        if(m_listNum.size()-1 < m_Curpos)
        {
            m_Curpos=0;
        }
        if(0==m_listNum.size())//没有成员时,关闭定时,停止抽奖
        {
            this->killTimer(m_MyTimeId);
            m_bDrawing=false;
            ui->BStartButton->setIcon(QIcon("D:\\linux\\pic\\51.jpg"));
            QMessageBox::information(this,"小老弟","奖抽完了,回家吧");
            return;
        }
        ui->Namelabel->setText(m_listNum.at(m_Curpos));
    }
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_BStartButton_clicked()
{
    if(m_bDrawing)
    {
        m_bDrawing=false;
        ui->BStartButton->setIcon(QIcon("D:\\linux\\pic\\22.png"));
        this->killTimer(m_MyTimeId);
        if(m_listNum.size()>0)
        {
            QString StrVal=m_listNum.at(m_Curpos);
            m_listNum.removeAt(m_Curpos);
            ui->listWidget->addItem(new QListWidgetItem(StrVal));
        }
    }
    else
    {
        m_bDrawing=true;
        ui->BStartButton->setIcon(QIcon("D:\\linux\\pic\\51.jpg"));
        m_MyTimeId=this->startTimer(TIMEOUT);
    }
}


void Widget::on_CloseButton_clicked()
{
    if(QMessageBox::Yes==QMessageBox::question(this,"温馨提示","你确定要退出抽奖吗?",QMessageBox::Yes|QMessageBox::No))
    {
        this->close();
    }
}

void Widget::mouseMoveEvent(QMouseEvent *e)
{
    if(e->buttons()==Qt::LeftButton)
    {
        if(m_dragging)
        {
            QPoint delta=e->globalPos()-m_startPosition;
            this->move(m_framePosition + delta);
        }
    }
}

void Widget::mousePressEvent(QMouseEvent *e)
{
    if(e->button()==Qt::LeftButton)
    {
        QRect rcTop=rect();
        rcTop.setBottom(rcTop.top()+TITLE_MOVE_HIGHT);
        if(rcTop.contains(e->pos()))
        {
            m_dragging=true;
            m_startPosition=e->globalPos();
            m_framePosition=this->frameGeometry().topLeft();//原点
            qDebug()<<"[mousePressEcent]:m_startPosition:"<

main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

你可能感兴趣的:(C++小项目,1024程序员节,qt,qt5,c++,开发语言)