#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QTimer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void textStreamReadFile();
void init_statusBar_label();
void init_timer();
private slots:
void onTimeout();
void switchTimeOut();
void on_actionopen_triggered();
void on_actionclose_triggered();
void on_pushButton_suspend_clicked();
void on_pushButton_start_clicked();
private:
Ui::MainWindow *ui;
QLabel *nameDisplayLabel;
QLabel* label_version;
QLabel* label_datetime;
QTimer* timer;
QStringList strListImport;
int count=0;
int test = 0;
};
#endif // MAINWINDOW_H
上面是mainwindow.hpp
main.cpp不变
下面mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDateTime>
#include <QDebug>
#include <QMessageBox>
#include <QFileDialog>
#include <QFile>
#include <QTime>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//连接界面上的关闭按钮与界面本身
//当界面按钮被点击时,自动调用界面的close()函数
connect(ui->pushButton_close,SIGNAL(clicked()),this,SLOT(close()));
QTime time = QTime::currentTime(); //获得当前时间
qsrand(time.msec()+time.second()*10000); //用时间初始化随机数种子
this->init_statusBar_label();
this->init_timer();
//ui->textEdit->setText("点名");
timer = new QTimer;//新建一个定时器
nameDisplayLabel = new QLabel(tr("随即点名器"));
this->connect(timer, SIGNAL(timeout()), this, SLOT(switchTimeOut()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::init_statusBar_label()
{
// 实例化一个标签对象
this->label_version = new QLabel;
// 把标签添加到状态栏,永久控件出现在状态栏的右侧
ui->statusBar->addPermanentWidget(this->label_version);
// 设置标签的文本值
this->label_version->setText("幸运大转盘 ");
this->label_datetime = new QLabel;
ui->statusBar->addPermanentWidget(this->label_datetime);
QDateTime cur = QDateTime::currentDateTime();
this->label_datetime->setText(cur.toString("yyyy/MM/dd hh:mm:ss ddd"));
}
void MainWindow::init_timer()
{
// 实例化定时器对象,同时指定父窗体,当父窗体销毁时,会自动释放定时器
this->timer = new QTimer(this);
// 设置间隔时间,单位为ms
this->timer->setInterval(1000);
// 连接超时信号和自己的槽函数
connect(this->timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
// 启动定时器
this->timer->start();
}
// 定时器超时,自动执行此槽函数
void MainWindow::onTimeout()
{
// 获取系统当前的日期和时间
QDateTime cur = QDateTime::currentDateTime();
// 把日期时间转成字符串,再显示到状态栏的标签上
this->label_datetime->setText(cur.toString("yyyy/MM/dd hh:mm:ss ddd"));
}
void MainWindow::textStreamReadFile()
{
// 清空文本框
ui->textEdit->clear();
// 通过文件对话框,获取一个文件名
QString fileName = QFileDialog::getOpenFileName(this);
if(!fileName.isEmpty())
{
QFile file(fileName);
if (file.open(QIODevice::ReadOnly|QIODevice::Text))//以只读方式打开文本信息
{
QTextStream textStream(&file);
textStream.setCodec("utf-8");
while(!textStream.atEnd())
{//一行一行的读取数据
QString temp = textStream.readLine();
if (!temp.isEmpty())//数据把不为空加入QStringList表中,且计数器加1
{
strListImport.append(temp);
count++;
}
}
QMessageBox::information(this, tr("提示:"), tr("您成功导入 %1 个名字!").arg(count));
}
}
else
QMessageBox::information(this, tr("提示:"), tr("导入失败,请检查格式是否正确!"));
}
void MainWindow::on_actionopen_triggered()
{
textStreamReadFile();
// 临时信息显示在状态栏的左侧
ui->statusBar->showMessage("open success");
}
void MainWindow::on_actionclose_triggered()
{
this->close();
}
void MainWindow::on_pushButton_suspend_clicked()
{
ui->textEdit->clear();
timer->stop();
ui->textEdit->append(strListImport.at(test));
strListImport.removeAt(test);//移除已被点名的人
count--;
qDebug()<<strListImport;
}
void MainWindow::switchTimeOut()//定时器溢出处理函数
{
test = qrand()%count;//初始化随机数
nameDisplayLabel->setText(strListImport.at(test));
ui->textEdit->append(strListImport.at(test));
qDebug() << "随机数:" << test << " count: " << count;
}
void MainWindow::on_pushButton_start_clicked()
{
if (count == 0)//若计数器为0,给出提示信息
QMessageBox::information(this, tr("提示:"), tr("您还没有导入数据!"));
else
{
timer->start(50);
qDebug()<<strListImport;
}
}
上图
注意图与代码的名字统一性