基于Qt的抽奖小程序

一:实现思路

Qt绘制抽奖界面

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

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

二:项目演示

抽奖小程序

二:源代码

widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QMessageBox>
#include<QPaintEvent>
#include<QPainter>
#include<QDebug>
#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:"<<m_startPosition<<endl;
            qDebug()<<"[mousePressEcent]:m_framePosition:"<<m_framePosition<<endl;
        }
    }
}

void Widget::mouseReleaseEvent(QMouseEvent *e)
{
    m_dragging=false;
}

main.cpp

#include "widget.h"
#include <QApplication>

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

    return a.exec();
}

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值