当前位置:   article > 正文

Qt 科目一考试系统(有源码)_c++qt实现驾考系统

c++qt实现驾考系统

项目源码和资源:科目一考试系统: qt实现科目一考试系统

 一.项目概述

该项目是一个基于Qt框架开发的在线考试系统,主要实现了考试题目的随机抽取、考试时间限制、成绩统计等功能。用户可以通过界面操作进行考试,并查看自己的考试成绩。
 

二.技术栈

Qt框架、C++语言、GUI设计、数据结构、算法等。

三.界面展示

1.登录界面

2.答题界面

3.提交界面

四.代码展示

1.核心要点

整体项目共分为两个部分,登录界面和答题界面,登录界面使用的qt设计器进行手动设计,登录界面核心的点在于使用正则表达式对已有账号进行分析,答题界面核心则是对文件中的试题进行处理。

 2.登录界面代码

  1. #ifndef LOGINDIALOG_H
  2. #define LOGINDIALOG_H
  3. #include <QDialog>
  4. QT_BEGIN_NAMESPACE
  5. namespace Ui { class LoginDialog; }
  6. QT_END_NAMESPACE
  7. class LoginDialog : public QDialog
  8. {
  9. Q_OBJECT
  10. public:
  11. LoginDialog(QWidget *parent = nullptr);
  12. ~LoginDialog();
  13. private slots:
  14. void on_pushButtonOk_clicked();
  15. void on_pushButtonCancel_clicked();
  16. private:
  17. Ui::LoginDialog *ui;
  18. private:
  19. void InitUI();
  20. };
  21. #endif // LOGINDIALOG_H
  1. #include "logindialog.h"
  2. #include "ui_logindialog.h"
  3. #include<QIcon>
  4. #include<QMessageBox>
  5. #include<QFile>
  6. #include<QTextStream>
  7. LoginDialog::LoginDialog(QWidget *parent)
  8. : QDialog(parent)
  9. , ui(new Ui::LoginDialog)
  10. {
  11. ui->setupUi(this);
  12. InitUI();
  13. }
  14. LoginDialog::~LoginDialog()
  15. {
  16. delete ui;
  17. }
  18. void LoginDialog::InitUI()
  19. {
  20. //设置窗口大小
  21. int wide=ui->labelimage->width();
  22. int height=ui->labelimage->height();
  23. this->setGeometry(600,300,wide,height);
  24. this->setWindowTitle("科目一考试系统");
  25. this->setWindowFlags(Qt::Dialog|Qt::WindowTitleHint);
  26. }
  27. void LoginDialog::on_pushButtonOk_clicked()
  28. {
  29. QRegExp rx("^[a-zA-Z][\\w]{5,17}@([A-Za-z0-9\\-]+\\.)+[A-Za-z]{2,6}$");
  30. bool res = rx.exactMatch(ui->lineEditaccount->text());
  31. if(!res){//匹配不成功
  32. QMessageBox::information(this,"提示","非法的邮箱地址,请你重新输入!");
  33. ui->lineEditaccount->clear();
  34. ui->lineEditpassword->clear();
  35. ui->lineEditaccount->setFocus();//设置光标
  36. return;
  37. }
  38. else{
  39. //匹配成功了
  40. QString filename; //账号密码数据文件
  41. QString strAccInput;//用户输入的账号
  42. QString strCode; //用户输入的密码
  43. QString strLine; //每次读取的一行数据
  44. QStringList strList;//保存分割读取的一行数据
  45. filename = "account.txt";//发布时要进行修改
  46. strAccInput = ui->lineEditaccount->text();//账号
  47. strCode = ui->lineEditpassword->text();//密码
  48. QFile file(filename);
  49. QTextStream stream(&file);
  50. if( file.open(QIODevice::ReadOnly | QIODevice::Text) )
  51. {
  52. while(!stream.atEnd()){
  53. strLine = stream.readLine();
  54. strList = strLine.split(",");
  55. if(strAccInput == strList.at(0))//账号匹配成功
  56. {
  57. if(strCode == strList.at(1)){//密码匹配成功
  58. QMessageBox::information(this,"提示","欢迎进入科目一考试系统!");
  59. file.close();
  60. done(Accepted);
  61. return;
  62. }else{
  63. QMessageBox::information(this,"提示","您输入的密码有误,请重新输入!");
  64. ui->lineEditpassword->clear();
  65. ui->lineEditpassword->setFocus();
  66. file.close();
  67. return;
  68. }
  69. }
  70. QMessageBox::information(this,"提示","您输入的账号有误,请重新输入!");
  71. ui->lineEditaccount->clear();
  72. ui->lineEditpassword->clear();
  73. ui->lineEditaccount->setFocus();
  74. file.close();
  75. return;
  76. }
  77. }else{
  78. QMessageBox::information(this,"提示","读取账号数据文件失败!");
  79. return;
  80. }
  81. }
  82. }
  83. void LoginDialog::on_pushButtonCancel_clicked()
  84. {
  85. //关闭窗口
  86. this->close();
  87. }

 3.答题界面代码

  1. #ifndef EXAMDIALOG_H
  2. #define EXAMDIALOG_H
  3. #include <QDialog>
  4. #include <QTimer>
  5. #include <QTextEdit>
  6. #include <QLabel>
  7. #include <QRadioButton>
  8. #include <QCheckBox>
  9. #include <QGridLayout>
  10. #include <QButtonGroup>
  11. class ExamDialog : public QDialog
  12. {
  13. Q_OBJECT
  14. public:
  15. ExamDialog(QWidget* parent = 0);
  16. void initTimer(); //初始化计时器
  17. void initLayout(); //初始化布局管理器
  18. bool initTextEdit();//初始化文本编辑器
  19. void initButtons(); //初始化按钮及标签
  20. bool hasNoSelect(); //判断题目是否有未完成的
  21. private:
  22. QTimer *m_timer; //计时器
  23. int m_timeGo; //考试已用时
  24. QTextEdit *m_textEdit; //考试题库显示
  25. QLabel *m_titleLabels[10]; //题目标签
  26. QButtonGroup *m_btnGroups[9]; //单项按钮分组
  27. QRadioButton *m_radioBtns[32]; //单选题按钮
  28. QCheckBox *m_checkBtns[4]; //多选题按钮
  29. QRadioButton *m_radioA; //判断题A选项
  30. QRadioButton *m_radioB; //判断题B选项
  31. QGridLayout *m_layout; //布局管理器
  32. QStringList m_answerList; //答案
  33. private slots:
  34. void freshTime(); //刷新考试时间
  35. void getScore(); //获取考试成绩
  36. };
  37. #endif // EXAMDIALOG_H
  1. #include "examdialog.h"
  2. #include <QFile>
  3. #include <QTextStream>
  4. #include <QMessageBox>
  5. #include <QApplication>
  6. #include <QPushButton>
  7. ExamDialog::ExamDialog(QWidget* parent):QDialog(parent)
  8. {
  9. //设置字体大小
  10. QFont font;
  11. font.setPointSize(12);
  12. setFont(font);
  13. //设置窗体背景颜色
  14. setPalette(QPalette(QColor(209,215,255)));
  15. setWindowTitle("考试已用时:0分0秒");
  16. setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
  17. resize(800,900);
  18. initTimer();
  19. initLayout();
  20. if(!initTextEdit()){
  21. QMessageBox::information(this,"提示","初始化题库数据文件失败!");
  22. QTimer::singleShot(0,qApp,SLOT(quit()));
  23. }
  24. initButtons();
  25. show();
  26. }
  27. void ExamDialog::initTimer()
  28. {
  29. m_timeGo = 0;
  30. m_timer = new QTimer(this);
  31. m_timer->setInterval(1000);
  32. m_timer->start();
  33. connect(m_timer,SIGNAL(timeout()),this,SLOT(freshTime()));
  34. }
  35. void ExamDialog::initLayout()
  36. {
  37. m_layout = new QGridLayout(this);
  38. m_layout->setSpacing(10); //设置控件间的间距
  39. m_layout->setMargin(10); //设置窗体与控件间的间隙
  40. }
  41. bool ExamDialog::initTextEdit()
  42. {
  43. QString strLine; //保存文件中读取到的一行数据
  44. QStringList strList; //保存读取到的答案行
  45. QString fileName("exam.txt");
  46. QFile file(fileName);
  47. QTextStream stream(&file);
  48. stream.setCodec("UTF-8");
  49. if( file.open(QIODevice::ReadOnly | QIODevice::Text) )
  50. {
  51. m_textEdit = new QTextEdit(this);
  52. m_textEdit->setReadOnly(true);
  53. QString strText; //用于保存显示到文本编辑器的数据
  54. int nLines = 0;
  55. while(!stream.atEnd()){
  56. //过滤首行
  57. if(nLines == 0){
  58. stream.readLine();
  59. nLines++;
  60. continue;
  61. }
  62. //过滤答案行
  63. if( (nLines >= 6 && nLines <= 6 * 9 && (nLines % 6 == 0) )
  64. || (nLines == 6 * 9 + 4) ){
  65. strLine = stream.readLine();
  66. strList = strLine.split(" ");
  67. m_answerList.append(strList.at(1));
  68. strText += "\n";
  69. nLines++;
  70. continue;
  71. }
  72. strText += stream.readLine();
  73. strText += "\n";
  74. nLines++;
  75. }
  76. m_textEdit->setText(strText);
  77. m_layout->addWidget(m_textEdit,0,0,1,10);
  78. file.close();
  79. return true;
  80. }else{
  81. return false;
  82. }
  83. }
  84. void ExamDialog::initButtons()
  85. {
  86. QStringList strList = {"A","B","C","D"};
  87. for(int i = 0; i <10; i++){
  88. //题目标签
  89. m_titleLabels[i] = new QLabel(this);
  90. m_titleLabels[i]->setText("第" + QString::number(i+1) + "题");
  91. m_layout->addWidget(m_titleLabels[i],1,i);
  92. //判断题
  93. if(i == 9){
  94. m_radioA = new QRadioButton(this);
  95. m_radioB = new QRadioButton(this);
  96. m_radioA->setText("正确");
  97. m_radioB->setText("错误");
  98. m_layout->addWidget(m_radioA,2,9);
  99. m_layout->addWidget(m_radioB,3,9);
  100. m_btnGroups[8] = new QButtonGroup(this);
  101. m_btnGroups[8]->addButton(m_radioA);
  102. m_btnGroups[8]->addButton(m_radioB);
  103. break;
  104. }
  105. if(i < 8) m_btnGroups[i] = new QButtonGroup(this);
  106. //选择题
  107. for(int j = 0; j < 4; j++)
  108. {
  109. if( i == 8){//多项多选题
  110. m_checkBtns[j] = new QCheckBox(this);
  111. m_checkBtns[j]->setText(strList.at(j));
  112. m_layout->addWidget(m_checkBtns[j],2+j,8);
  113. }else{//单项选择题
  114. m_radioBtns[4 * i + j] = new QRadioButton(this);
  115. m_radioBtns[4 * i + j]->setText(strList.at(j));
  116. m_layout->addWidget(m_radioBtns[4 * i + j],2+j,i);
  117. m_btnGroups[i]->addButton(m_radioBtns[4 * i + j]);
  118. }
  119. }
  120. }
  121. QPushButton *submitBtn = new QPushButton(this);
  122. submitBtn->setText("提交");
  123. submitBtn->setFixedSize(100,35);
  124. connect(submitBtn,SIGNAL(clicked(bool)),this,SLOT(getScore()));
  125. m_layout->addWidget(submitBtn,6,9);
  126. }
  127. bool ExamDialog::hasNoSelect()
  128. {
  129. int radioSelects = 0;
  130. for(int i = 0; i < 8; i++)
  131. {
  132. if( m_btnGroups[i]->checkedButton() )
  133. radioSelects++;
  134. }
  135. if(radioSelects != 8)
  136. return true;
  137. int checkSelects = 0;
  138. for(int i = 0; i < 4; i++)
  139. {
  140. if(m_checkBtns[i]->isChecked())
  141. checkSelects++;
  142. }
  143. if(checkSelects == 0 || checkSelects == 1)
  144. return true;
  145. if(!m_radioA->isChecked() && !m_radioB->isChecked())
  146. return true;
  147. return false;
  148. }
  149. void ExamDialog::freshTime()
  150. {
  151. m_timeGo++;
  152. QString min = QString::number(m_timeGo / 60);
  153. QString sec = QString::number(m_timeGo % 60);
  154. setWindowTitle("考试已用时:" + min + "分" + sec + "秒");
  155. }
  156. void ExamDialog::getScore()
  157. {
  158. if(hasNoSelect()){
  159. QMessageBox::information(this,"提示","您有未完成的题目,请完成考试!","是");
  160. return;
  161. }
  162. int scores = 0;
  163. for(int i = 0; i < 10; i++)
  164. {
  165. //单选题计分
  166. if( i < 8)
  167. if(m_btnGroups[i]->checkedButton()->text() == m_answerList.at(i))
  168. scores += 10;
  169. //多项选择题计分
  170. if(i == 8){
  171. QString answer = m_answerList.at(i);
  172. bool hasA = false;
  173. bool hasB = false;
  174. bool hasC = false;
  175. bool hasD = false;
  176. if( answer.contains("A") ) hasA = true;
  177. if( answer.contains("B") ) hasB = true;
  178. if( answer.contains("C") ) hasC = true;
  179. if( answer.contains("D") ) hasD = true;
  180. bool checkA = m_checkBtns[0]->checkState();
  181. bool checkB = m_checkBtns[1]->checkState();
  182. bool checkC = m_checkBtns[2]->checkState();
  183. bool checkD = m_checkBtns[3]->checkState();
  184. if( hasA != checkA) continue;
  185. if( hasB != checkB) continue;
  186. if( hasC != checkC) continue;
  187. if( hasD != checkD) continue;
  188. scores += 10;
  189. }
  190. //判断题计分
  191. if(i == 9){
  192. if(m_btnGroups[8]->checkedButton()->text() == m_answerList.at(i))
  193. scores += 10;
  194. }
  195. }
  196. QString str = "您的分数是:" + QString::number(scores) + "分,是否重新考试?";
  197. int res = QMessageBox::information(this,"提示",str,QMessageBox::Yes | QMessageBox::No);
  198. if(res == QMessageBox::Yes)
  199. return;
  200. else
  201. close();
  202. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/848908
推荐阅读
相关标签
  

闽ICP备14008679号