QT实现年会抽奖小软件的示例代码

 更新时间:2022年01月21日 10:24:47   作者:KevinQUI  
本文主要介绍了QT实现年会抽奖小软件的示例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、效果展示:

1、操作说明

下拉选择主题,点击开始按钮,开始滚动,再次点击停止,显示幸运之星及名称。中选人员不参与接下来的抽取,除非软件重启或点击复位按钮。

二、软件代码介绍

1、工程目录

2、核心代码之主类代码部分

main.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QCoreApplication>
#include <QDesktopWidget>
#include <QTimer>
#include <QPixmap>
#include <QSize>
#include "thread.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT
    Thread *thread;
    QTimer *timer;
    QTimer *timer2;

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_startBtn_clicked();
    void time2out();
    void show_image();
    void on_ResetBtn_clicked();
    void on_selectCase_activated(const QString &arg1);

private:
    Ui::MainWindow *ui;
    void Init();
    void openTxtFile(QString name);
    void randomSelect();
    void clickStop();
    int g_val,index;
    bool start;
    QStringList strList_store0,strList;
    QImage *img;

    const QString txt_Dir = (QCoreApplication::applicationDirPath() + "/cfg/");
    QString pic_Dir = (QCoreApplication::applicationDirPath() + "/photo/");

};
#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QFileDialog"
#include "QMessageBox"
#include <QDebug>
#include <qmath.h>
#include <QtMath>
#include <QRandomGenerator>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Init();
    thread = new Thread;
    timer2 = new QTimer(this);
    connect(timer2,SIGNAL(timeout()),this,SLOT(time2out()));
}
MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::Init(){
    int w = QApplication::desktop()->width();
    int h = QApplication::desktop()->height();
    this->setFixedSize(w,h);
    this->setWindowState(Qt::WindowMaximized);
    this->setWindowTitle("幸运之星");
    this->setObjectName("mainWindow");
    this->setStyleSheet("#mainWindow{border-image:url(:/background/bg.jpg);}");

    start = false;
    ui->caseName->setText("活动之主题");
    ui->caseName->setStyleSheet("font-size:60px;font-weight:500;color:yellow");
    ui->name->setStyleSheet("font-size:60px;font-weight:500;color:yellow");
    ui->startBtn->setStyleSheet("background:#f0f;");
    ui->ResetBtn->setStyleSheet("background:#f0f;");
    ui->selectCase->setStyleSheet("background:#f0f;");
    QDir dir(txt_Dir);
    QStringList nameFilters;
    nameFilters<<"*.txt";
    QStringList fileList = dir.entryList(nameFilters,QDir::Files|QDir::Readable,QDir::Name);
    //qDebug()<<"len:"<<fileList.length();
    for (int i=0;i<fileList.length();i++) {
        QFileInfo f = fileList.at(i);
        //qDebug()<<"文件名:"<<f.fileName();
        if(f.fileName()!=nullptr)
        {
            openTxtFile(txt_Dir+f.fileName());
        }else{
            qDebug()<<"多个文件";
        }
    }
}
void MainWindow::openTxtFile(QString filename){

    QFile file(filename);//从文件目录读取json配置文件
    if(!file.open(QIODevice::ReadOnly))
    {
       QMessageBox::warning(this,"Error",QString::fromLocal8Bit("无法打开配置文件!"),QMessageBox::Close);
       return;
    }

    QList<QString> list;

    QString  l  = file.readAll();
    //qDebug()<<"内容:"<<l;
    strList_store0 = l.split("\r\n");
    strList = strList_store0;

}
void MainWindow::on_startBtn_clicked()
{
    if(!start)
    {
        ui->startBtn->setText("停止");
        start = true;
        timer2->start(50);
        thread->start();

    }else{
        ui->startBtn->setText("开始");
        start = false;
        clickStop();
    }
}
void MainWindow::randomSelect()
{
    int len;
    len= strList.length();
    if(len>1)
    {
        show_image();
    }
}
void MainWindow::show_image()
{
    img=new QImage; //新建一个image对象
    QString path2;
    int len =  strList.length();
    index = rand()%len;
    path2 = pic_Dir+strList.at(index)+".png";
    qDebug()<<"path2:"<<path2;
    img->load(path2); //将图像资源载入对象img,注意路径,可点进图片右键复制路径

    ui->image->setScaledContents(true);
    img->scaled(ui->image->size(),Qt::KeepAspectRatio);//Qt::SmoothTransformation
    ui->image->setPixmap(QPixmap::fromImage(*img));
    //val =  qrand()%(len);
    qDebug()<<"val:"<<index;

    ui->name->setText(strList.at(index));
    delete img;
}
//出结果
void MainWindow::clickStop()
{

    thread->terminate();
    timer2->stop();
    strList.removeAt(index);
    int list_Len = strList.length();
    if(list_Len<2)
    {
        qDebug()<<"val:"<<index;
        QMessageBox::warning(this,"Error",("请复位后再操作!"),QMessageBox::Close);
    }
}
//滚动
void MainWindow::time2out(){
    randomSelect();
}

void MainWindow::on_ResetBtn_clicked()
{
    strList = strList_store0;

}

void MainWindow::on_selectCase_activated(const QString &arg1)
{
    ui->caseName->setText(arg1);

}

3、核心代码之线程类代码部分

class Thread : public QThread
{

Q_OBJECT
public:
   explicit Thread(QObject *parent = 0);
   void run();
signals:
   void show_image();
public slots:
};

void Thread::run()
{
    while(true)
    {
        emit show_image();
        usleep(100000);
    }

}

到此这篇关于QT实现年会抽奖小软件的示例代码的文章就介绍到这了,更多相关QT 抽奖内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • c语言B树深入理解

    c语言B树深入理解

    B树是为磁盘或其他直接存储设备设计的一种平衡查找树,本文将详细介绍c语言B树,需要的朋友可以参考下
    2012-11-11
  • 你知道如何自定义sort函数中的比较函数

    你知道如何自定义sort函数中的比较函数

    这篇文章主要介绍了如何自定义sort函数中的比较函数,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • C语言学生成绩管理系统课程设计word版

    C语言学生成绩管理系统课程设计word版

    这篇文章主要为大家详细介绍了C语言学生成绩管理课程设计,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12
  • C++中的内存分区介绍

    C++中的内存分区介绍

    这篇文章主要介绍了C++中的内存分区介绍,C++的内存划分为栈区、堆区、全局区/静态区、字符串常量和代码区,本文分别对他们一一说明,需要的朋友可以参考下
    2015-07-07
  • C++ move()函数及priority_queue队列使用记录

    C++ move()函数及priority_queue队列使用记录

    move(obj)函数的功能是把obj当做右值处理,可以应用在对象的移动上,这篇文章主要介绍了C++ move()函数及priority_queue队列使用记录,需要的朋友可以参考下
    2023-01-01
  • Qt使用QListWidget实现自定义Item

    Qt使用QListWidget实现自定义Item

    这篇文章主要为大家详细介绍了Qt如何使用QListWidget实现自定义Item的效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-10-10
  • 基于c语言知识点的补遗介绍

    基于c语言知识点的补遗介绍

    本篇文章是对c语言知识点的一些补遗进行详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C语言实现简单的扫雷小游戏

    C语言实现简单的扫雷小游戏

    这篇文章主要为大家详细介绍了C语言实现简单的扫雷小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-10-10
  • c++多线程之死锁的发生的情况解析(包含两个归纳,6个示例)

    c++多线程之死锁的发生的情况解析(包含两个归纳,6个示例)

    这篇文章主要介绍了c++多线程之死锁的发生的情况解析(包含两个归纳,6个示例),需要的朋友可以参考下
    2018-01-01
  • C语言 fseek(f,0,SEEK_SET)函数案例详解

    C语言 fseek(f,0,SEEK_SET)函数案例详解

    这篇文章主要介绍了C语言 fseek(f,0,SEEK_SET)函数案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08

最新评论