Qt - 驾校科目-考试系统-窗口交互
如果登录成功
编译执行
这样子就能避免一闪而过(方法一)
编译执行也没有问题
内存分配方法(二)
#ifndef EXANDIALOG_H
#define EXANDIALOG_H
#include <QDialog>
#include <QTimer>
#include <QTextEdit>
#include <QLabel>
#include <QRadioButton>
#include <QCheckBox>
#include <QGridLayout>
#include <QStringList>
#include <QWidget>
#include <QButtonGroup>
class ExanDialog : public QDialog
{
Q_OBJECT
public:
ExanDialog(QWidget* parent = 0);
void initTimer(); //初始化计时器
void initLayout(); //初始化布局
bool initTextEdit(); //初始化文本编辑器
void initButtons(); //初始化按钮及标签
bool hasNoSelect(); //判断题目是否有未完成的
private:
QTimer *m_timer; //计时器
int m_timeGo; //考试已用时
QTextEdit *m_textEdit; //考试题库显示
QLabel *m_titleLabels[10]; //题目标签
QButtonGroup *m_btnGroups[9]; //单选按钮分组
QRadioButton *m_radioBtns[32]; //单选题按钮
QCheckBox *m_checkBtns[4]; //多选题按钮
QRadioButton *m_radioA; //判断题A选项
QRadioButton *m_radioB; //判断题B选项
QGridLayout *m_layout; //布局管理器
QStringList m_answerList; //答案
private slots:
void freshTime(); //刷新考试时间
void getScore(); //获取考试成绩
};
#endif // EXANDIALOG_H
#include "exandialog.h"
#include <QString>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QApplication>
#include <QPushButton>
ExanDialog::ExanDialog(QWidget* parent):QDialog(parent)
{
//设置字体大小
QFont font;
font.setPointSize(12);
setFont(font);
//设置窗体背景颜色
setPalette(QPalette(QColor(209, 215, 255)));
setWindowTitle("考试已用时: 0分0秒");
setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
resize(800, 900);
initTimer();
initLayout();
if (!initTextEdit())
{
QMessageBox::information(this, "提示", "初始化题库数据文件失败! ");
QTimer::singleShot(0, qApp, SLOT(quit()));
}
initButtons();
show();
}
void ExanDialog::initTimer()
{
this->m_timeGo = 0;
this->m_timer = new QTimer(this);
this->m_timer->setInterval(1000);
this->m_timer->start();
connect(m_timer, SIGNAL(timeout()), this, SLOT(freshTime()));
}
void ExanDialog::initLayout()
{
this->m_layout =new QGridLayout(this);
this->m_layout->setSpacing(10); //设置控件间的间距
this->m_layout->setMargin(10); //设置窗体与控件的间隔
}
bool ExanDialog::initTextEdit()
{
QString strLine; //保存文件中读取到的一行数据
QStringList strList; //保存读取到的答案行
QString fileName("../exam.txt");
QFile file(fileName);
QTextStream stream(&file);
stream.setCodec("UTF-8");
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
m_textEdit = new QTextEdit(this);
m_textEdit->setReadOnly(true);
QString strText; //用于保存显示到文本编辑器的数据
int nLines = 0;
while(!stream.atEnd())
{
//过滤首行
if (nLines == 0)
{
stream.readLine();
nLines++;
continue;
}
//过滤答案行
if ( (nLines >=6 && nLines <=6 * 9 && (nLines % 6 == 0)) || (nLines == 6 * 9 + 4))
{
strLine = stream.readLine();
strList = strLine.split(" ");
m_answerList.append(strList.at(1));
strText += "\n";
nLines++;
continue;
}
strText += stream.readLine();
strText += "\n";
nLines++;
}
m_textEdit->setText(strText);
m_layout->addWidget(m_textEdit, 0, 0, 1, 10);
file.close();
return true;
}else{
return false;
}
}
void ExanDialog::initButtons()
{
QStringList strList = {"A", "B", "C", "D"};
for (int i = 0; i<10; i++)
{
//题目标签
m_titleLabels[i] = new QLabel(this);
m_titleLabels[i]->setText("第"+QString::number(i+1)+"题");
m_layout->addWidget(m_titleLabels[i], 1, i);
//判断题
if (i==9)
{
m_radioA = new QRadioButton(this);
m_radioB = new QRadioButton(this);
m_radioA->setText("正确");
m_radioB->setText("错误");
m_layout->addWidget(m_radioA, 2, 9);
m_layout->addWidget(m_radioB, 3, 9);
m_btnGroups[8] = new QButtonGroup(this);
m_btnGroups[8]->addButton(m_radioA);
m_btnGroups[8]->addButton(m_radioB);
break;
}
if (i < 8) m_btnGroups[i] = new QButtonGroup(this);
//选择题
for(int j=0; j<4; j++)
{
//多项选择题(先判断特性的先)
if(i==8)
{
m_checkBtns[j]=new QCheckBox(this);
m_checkBtns[j]->setText(strList.at(j));
m_layout->addWidget(m_checkBtns[j], 2+j, 8);
}else //单项选择题
{
m_radioBtns[4 * i + j] = new QRadioButton(this);
m_radioBtns[4 * i + j]->setText(strList.at(j));
m_layout->addWidget(m_radioBtns[4 * i + j], 2+j, i);
m_btnGroups[i]->addButton(m_radioBtns[4 * i + j]);
}
}
}
QPushButton *submitBtn = new QPushButton(this);
submitBtn->setText("提交");
submitBtn->setFixedSize(100, 35);
connect(submitBtn, SIGNAL(clicked(bool)), this, SLOT(getScore()));
m_layout->addWidget(submitBtn, 6, 9);
}
bool ExanDialog::hasNoSelect()
{
int radioSelects = 0;
for (int i =0; i < 8; i++)
{
if ( m_btnGroups[i]->checkedButton() )
{
radioSelects++;
}
}
if(radioSelects != 8)
{
return true; //考试不能结束
}
int checkSelect = 0;
for (int i = 0; i < 4; i++)
{
if (m_checkBtns[i]->isChecked())
{
checkSelect++;
}
}
if (checkSelect == 0 || checkSelect == 1)
{
return true;
}
if (!m_radioA->isChecked() && !m_radioB->isChecked())
{
return true;
}
return false;
}
void ExanDialog::freshTime()
{
m_timeGo++;
QString min = QString::number(m_timeGo/60);
QString sec = QString::number(m_timeGo%60);
setWindowTitle("考试已用时: " + min + "分" + sec + "秒");
}
void ExanDialog::getScore()
{
if (hasNoSelect())
{
QMessageBox::information(this, "提示", "您有未完成的题目, 请完成考试! ", "是");
return;
}
int scores = 0;
for (int i = 0; i < 10; i++)
{
if (i < 8) //单选选择题计分
{
if (m_btnGroups[i]->checkedButton()->text() == m_answerList.at(i))
{
scores += 10;
}
}
if (i == 8) //多选选择题计分
{
QString answer = m_answerList.at(i); //提取多选答案
bool hasA = false;
bool hasB = false;
bool hasC = false;
bool hasD = false;
if ( answer.contains("A") ) hasA = true;
if ( answer.contains("B") ) hasB = true;
if ( answer.contains("C") ) hasC = true;
if ( answer.contains("D") ) hasD = true;
bool checkA = m_checkBtns[0]->checkState();
bool checkB = m_checkBtns[1]->checkState();
bool checkC = m_checkBtns[2]->checkState();
bool checkD = m_checkBtns[3]->checkState();
if ( hasA != checkA ) continue;
if ( hasB != checkB ) continue;
if ( hasC != checkC ) continue;
if ( hasD != checkD ) continue;
scores += 10;
}
if (i == 9) //判断题计分
{
if (m_btnGroups[8]->checkedButton()->text() == m_answerList.at(i))
{
scores += 10;
}
}
}
QString str = "您的分数是: " + QString::number(scores) + "分, 是否重新考试? ";
int res = QMessageBox::information(this, "提示", str, QMessageBox::Yes | QMessageBox::No);
if (res == QMessageBox::Yes)
{
return;
}
else
{
close();
}
}
#ifndef LOGINDIALOG_H
#define LOGINDIALOG_H
#include <QDialog>
namespace Ui {
class LoginDialog;
}
class LoginDialog : public QDialog
{
Q_OBJECT
public:
explicit LoginDialog(QWidget *parent = 0);
~LoginDialog();
private slots:
void on_loginBtn_clicked();
void on_cancelBtn_clicked();
private:
Ui::LoginDialog *ui;
};
#endif // LOGINDIALOG_H
#include "logindialog.h"
#include "ui_logindialog.h"
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
LoginDialog::LoginDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::LoginDialog)
{
ui->setupUi(this);
ui->imgLabel->setScaledContents(true);
this->resize(ui->imgLabel->width(), ui->imgLabel->height());
this->setFixedSize(width(), height());
this->setWindowTitle("驾校科目一考试登录");
this->setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
}
LoginDialog::~LoginDialog()
{
delete ui;
}
void LoginDialog::on_loginBtn_clicked()
{
//正则验证邮箱地址 用户名@域名 888888@qq.com
//元字符解析: ^表示规则字符串的开始 $表示规则字符串的结束
//+表示匹配次数>=1次 *表示匹配任意次数(可为0次) {n, m} 表示匹配次数至少n次, 至多m次
QRegExp rx("^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}$");
bool res = rx.exactMatch(ui->accountEdit->text());
if (res==false) //匹配不成功
{
QMessageBox::information(this, "提示", "非法的邮箱地址, 请你重新输入! ");
ui->accountEdit->close();
ui->accountEdit->setFocus();
return;
}
else
{
QString filename; //账号密码数据文件
QString strAccInput; //用户输入的账号
QString strCode; //用户输入的密码
QString strLine; //每次读取的一行数据
QStringList strList; //保存分割读取的第一行数据
filename="../account.txt";
strAccInput = ui->accountEdit->text();
strCode = ui->codeEdit->text();
QFile file(filename);
QTextStream stream(&file);
if(file.open(QIODevice::ReadOnly | QIODevice::Text)) //打开成功
{
while(!stream.atEnd())
{
strLine = stream.readLine();
strList = strLine.split(",");
if (strAccInput==strList.at(0)) //验证账号
{
if(strCode==strList.at(1)) //验证密码
{
QMessageBox::information(this, "提示", "欢迎进入科目一考试系统! ");
file.close();
done(Accepted);
return;
}
else
{
QMessageBox::information(this, "提示", "您输入的密码有误, 请重新输入! ");
ui->codeEdit->clear();
ui->codeEdit->setFocus();
file.close();
return;
}
}
}
QMessageBox::information(this, "提示", "您输入的账号有误, 请重新输入! ");
ui->accountEdit->clear();
ui->codeEdit->clear();
ui->accountEdit->setFocus();
file.close();
return;
}
else //打开失败
{
QMessageBox::information(this, "提示", "读取账号数据文件失败!");
return;
}
}
}
void LoginDialog::on_cancelBtn_clicked()
{
done(Rejected);
}
#include "logindialog.h"
#include <QApplication>
#include "exandialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LoginDialog logDialog;
int res = logDialog.exec();
if (res == QDialog::Accepted)
{
ExanDialog *examDialog;
examDialog = new ExanDialog;
}else
{
return 0;
}
return a.exec();
}
结语:
时间: 2020-07-21