QT實現(xiàn)年會抽獎小軟件的示例代碼
一、效果展示:
1、操作說明
下拉選擇主題,點擊開始按鈕,開始滾動,再次點擊停止,顯示幸運之星及名稱。中選人員不參與接下來的抽取,除非軟件重啟或點擊復(fù)位按鈕。
二、軟件代碼介紹
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()<<"內(nèi)容:"<<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,注意路徑,可點進圖片右鍵復(fù)制路徑 ? ? 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; } //出結(jié)果 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",("請復(fù)位后再操作!"),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); ? ? } }
到此這篇關(guān)于QT實現(xiàn)年會抽獎小軟件的示例代碼的文章就介紹到這了,更多相關(guān)QT 抽獎內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
你知道如何自定義sort函數(shù)中的比較函數(shù)
這篇文章主要介紹了如何自定義sort函數(shù)中的比較函數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12C語言學(xué)生成績管理系統(tǒng)課程設(shè)計word版
這篇文章主要為大家詳細介紹了C語言學(xué)生成績管理課程設(shè)計,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-12-12C++?move()函數(shù)及priority_queue隊列使用記錄
move(obj)函數(shù)的功能是把obj當(dāng)做右值處理,可以應(yīng)用在對象的移動上,這篇文章主要介紹了C++?move()函數(shù)及priority_queue隊列使用記錄,需要的朋友可以參考下2023-01-01Qt使用QListWidget實現(xiàn)自定義Item
這篇文章主要為大家詳細介紹了Qt如何使用QListWidget實現(xiàn)自定義Item的效果,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10c++多線程之死鎖的發(fā)生的情況解析(包含兩個歸納,6個示例)
這篇文章主要介紹了c++多線程之死鎖的發(fā)生的情況解析(包含兩個歸納,6個示例),需要的朋友可以參考下2018-01-01C語言 fseek(f,0,SEEK_SET)函數(shù)案例詳解
這篇文章主要介紹了C語言 fseek(f,0,SEEK_SET)函數(shù)案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08