当前位置:网站首页>Qt入门(六)——抽奖系统的实现
Qt入门(六)——抽奖系统的实现
2022-08-10 23:40:00 【光追雨】
一、ui设计
1.1 图像展示
1.2 类名定义
二、代码展示
2.1 MainWindow.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QIcon>
#include <QMessageBox>
#include <QPainter>
#include <QPixmap>
#include <QPoint>
#include <QMouseEvent>
#define TITLE_MOVE_HIGHT 200
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
void timerEvent(QTimerEvent *e); //时间事件函数
void paintEvent(QPaintEvent *e); //画布背景函数
void mousePressEvent(QMouseEvent *e); //判断鼠标按压
void mouseMoveEvent(QMouseEvent *e); //判断鼠标拖拽
void mouseReleaseEvent(QMouseEvent *e); //判断鼠标是否松开
~Widget();
private slots:
void on_StartButton_clicked(); //开始按钮
void on_CloseButton_clicked(); //退出按钮
private:
bool m_pressing; //鼠标是否正在按压——标志位
QPoint m_startPosition; //获取鼠标刚开始按压时,鼠标在全局的位置信息
QPoint m_framePosition; //获取鼠标刚开始按压时,窗口(左上角)的位置信息
private:
Ui::Widget *ui;
bool m_Drawing; //判断是否正在抽奖
int myTimerId; //确定时间ID
QStringList m_listNum; //定义待抽奖名单
int m_CurPos; //定义名单索引
QPixmap m_pix; //定义背景图片
};
#endif // WIDGET_H
2.2 main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
2.3 MainWindow.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_pressing = false;
m_Drawing = false;
//不显示标题栏
this->setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
m_pix.load("C:/Users/DELL/Desktop/lottery/img/background.jpg");
ui->listWidget->setStyleSheet("background:gray");
ui->StartButton->setIcon(QIcon("C:/Users/DELL/Desktop/lottery/img/start.jpg"));
ui->CloseButton->setStyleSheet("QPushButton{border-image: url(C:/Users/DELL/Desktop/lottery/img/close1.jpg);}"
"QPushButton:hover{border-image: url(C:/Users/DELL/Desktop/lottery/img/close2.jpg);}"
"QPushButton:pressed{border-image: url(C:/Users/DELL/Desktop/lottery/img/close3.jpg);}");
m_listNum.push_back("吃饭");
m_listNum.push_back("睡觉");
m_listNum.push_back("打游戏");
m_listNum.push_back("锻炼");
m_listNum.push_back("运动");
m_listNum.push_back("跑步");
m_listNum.push_back("读书");
m_listNum.push_back("深度之眼");
m_listNum.push_back("进程间通信");
}
Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *e)
{
if(myTimerId == e->timerId())
{
m_CurPos++;
if(m_listNum.size() - 1 <= m_CurPos)
{
m_CurPos = 0;
}
if(0 == m_listNum.size())
{
killTimer(myTimerId);
m_Drawing = false;
ui->StartButton->setIcon(QIcon("C:/Users/DELL/Desktop/lottery/img/start.jpg"));
QMessageBox::warning(this,"温馨提示","已经抽完了");
return;
}
ui->Listlabel->setText(m_listNum.at(m_CurPos));
}
}
void Widget::on_StartButton_clicked()
{
if(!m_Drawing)
{
m_Drawing = true;
ui->StartButton->setIcon(QIcon("C:/Users/DELL/Desktop/lottery/img/close1.jpg"));
myTimerId = this->startTimer(20);
}
else
{
m_Drawing = false;
ui->StartButton->setIcon(QIcon("C:/Users/DELL/Desktop/lottery/img/start.jpg"));
killTimer(myTimerId);
if(m_listNum.size() > 0)
{
QString strval = m_listNum.at(m_CurPos);
m_listNum.removeAt(m_CurPos);
ui->listWidget->addItem(new QListWidgetItem(strval));
}
}
}
void Widget::on_CloseButton_clicked()
{
if(QMessageBox::Yes == QMessageBox::question(this,"提示","你确定要退出此程序吗?",QMessageBox::Yes | QMessageBox::No))
{
this->close();
}
}
void Widget::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
QRect rc = rect();
painter.drawPixmap(rc,m_pix);
}
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_pressing = true;
m_startPosition = e->globalPos();
m_framePosition = frameGeometry().topLeft();
}
}
}
void Widget::mouseMoveEvent(QMouseEvent *e) //判断鼠标拖拽
{
if(e->buttons() == Qt::LeftButton)
{
if(m_pressing)
{
QPoint delta = e->globalPos() - m_startPosition;
move(m_framePosition + delta);
}
}
}
void Widget::mouseReleaseEvent(QMouseEvent *e) //判断鼠标是否松开
{
m_pressing = false;
}
三、成果展示
边栏推荐
- Kubernetes 选举机制HA
- 点云中的一些名词解释
- C language, operators of shift operators (> >, < <) explanation
- Cache knowledge summary
- 三栏布局实现
- u盘数据不小心删除怎么恢复,u盘数据删除如何恢复
- [C language articles] Expression evaluation (implicit type conversion, arithmetic conversion)
- 2. 依赖管理和自动配置
- Deep Learning Transformer Architecture Analysis
- ROS实验笔记之——安装QPEP以及Intel-MKL
猜你喜欢
安科瑞为工业能效行动计划提供EMS解决方案-Susie 周
How to recover data from accidentally deleted U disk, how to recover deleted data from U disk
点云中的一些名词解释
Deep Learning Transformer Architecture Analysis
C194铜合金C19400铁铜合金
web 性能提升(将持续更新……)
高性能MySQL核心整理强势来袭
[C language] First understanding of pointers
CW614N铜棒CuZn39Pb3对应牌号
ROS Experimental Notes - Install QPEP and Intel-MKL
随机推荐
Design and Realization of Employment Management System in Colleges and Universities
CW614N铜棒CuZn39Pb3对应牌号
iNFTnews | Web3时代,用户将拥有数据自主权
Summary of Confused Knowledge Points for "High Items" in the Soft Examination in the Second Half of 2022 (2)
线程相关知识点
Rust从入门到精通05-语句和表达式
How to recover data from accidentally deleted U disk, how to recover deleted data from U disk
5. Lombok
SQL注入基础
ROS Experiment Notes - Validation of UZH-FPV Dataset
多语种翻译-多语种翻译软件免费
Google Chrome73~81版本浏览器的跨域问题解决方案
如何做专利挖掘,关键是寻找专利点,其实并不太难
9. Rest style request processing
Server Tips
CSDN21天学习挑战赛之折半查找
SAS数据处理技术(一)
英文文献阅读时,如何做笔记?
2.0966 铝青铜板CuAl10Ni5Fe4铜棒
翻译软件哪个准确度高【免费】