• 07——驾校科目一考试系统——布局题库


    如果需要题库的资料(exam.txt)可以留下邮箱,博主会发给大家的。

    总代码

    先给大家线上将题库布局好的总代码,本节不需要像之前那样去拖动ui设计,直接使用代码进行实现。后续会将重点知识点总结起来,便与复习与学习。

    examdialog.h

    //examdialog.h 
    #ifndef EXAMDIALOG_H
    #define EXAMDIALOG_H
    
    #include<QDialog>
    #include<QTimer>
    #include <QTextEdit>
    #include <QLabel>
    #include <QRadioButton>
    #include <QCheckBox>
    #include <QGridLayout>
    #include <QButtonGroup>
    
    class ExamDialog : public QDialog
    {
        //因为我们后期需要使用到 信号与槽机制,所以添加如下代码
        Q_OBJECT
    
    public:
        ExamDialog(QWidget *parent = 0);
    
        //正式编写代码了: Qt 命名规则,数据成员一般以 m_开头。类名首写字母以大写字母开头。成员方法一般以小写字母开头。
    
        void initTimer();   //初始化计时器
        void initLayout();  //初始化布局管理器
        bool initTextEdit();//初始化文本编辑器  --->    因为读取文件可能失败,所以给一个返回值
    
        //刷新考试时间:思路:使用QT自带的计时器
        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(); //添加私有的一个槽方法
    };
    
    #endif // EXAMDIALOG_H
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    在这里插入图片描述

    examdialog.cpp

    实现 examdialog.h定义的方法
    
    • 1
    //examdialog.cpp 
    #include "examdialog.h"
    #include <QFile>
    #include <QTextStream>
    #include <QMessageBox>
    #include <QApplication>
    #include <QPushButton>
    
    ExamDialog::ExamDialog(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,"提示","初始化题库数据文件失败!");
            //因为初始化失败,所以系统就直接退出(发送信号)。
            //第一个参数:多久来发送信号(0:立即退出)  第二个参数:想要那个对象相应槽方法(当前应用程序:QApp[应用程序的全局对象])  第三个参数:响应方法
            QTimer::singleShot(0,qApp,SLOT(quit()));//间隔0s,当前的应用程序执行退出操作
        }
    
        show();
    }
    
    void ExamDialog::initTimer()
    {
        m_timeGo = 0;
        m_timer = new QTimer(this);
        m_timer->setInterval(1000);
        m_timer->start();
    
        connect(m_timer,SIGNAL(timeout()),this,SLOT(freshTime()));
    }
    
    void ExamDialog::initLayout()
    {
        m_layout = new QGridLayout(this);
        m_layout->setSpacing(10);   //设置控件间的间距
        m_layout->setMargin(10);    //设置窗体与控件间的间隙
    }
    
    bool ExamDialog::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;
                }
    
                //过滤答案行(题目、ABCD、答案一共是6行)。一共有10个题。第十题是判断题需要特殊的处理。
                if( (nLines >= 6 && nLines <= 6 * 9 && (nLines % 6 == 0) )  /*选择题的答案*/
                        || (nLines == 6 * 9 + 4)/*判断题的答案行*/){
                    //对于答案行的处理
                    strLine = stream.readLine();    //先读取这一行
                    strList = strLine.split(" ");   //对答案进行处理——以空格进行分割(eg:答案 A)
                    m_answerList.append(strList.at(1));//把答案存放到答案链表里边 因为0:答案 1:A\b\c\d
                    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 ExamDialog::freshTime()
    {
        m_timeGo++;
        QString min = QString::number(m_timeGo / 60);
        QString sec = QString::number(m_timeGo % 60);
        setWindowTitle("考试已用时:" + min + "分" + sec + "秒");
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115

    在这里插入图片描述

    在构造函数中进行初始化
    在这里插入图片描述

    运行效果

    因为上一章博客将登录界面暂时隐藏掉了,所以,运行程序就会直接是这个样子
    在这里插入图片描述

    重要知识点总结(从代码中提炼出来的)

    初始化布局

    	m_layout = new QGridLayout(this);	
    	m_layout->setSpacing(10);   //设置控件间的间距
        m_layout->setMargin(10);    //设置窗体与控件间的间隙
    
    • 1
    • 2
    • 3

    打开文件并设置字符集

    	QString fileName("exam.txt");
        QFile file(fileName);
        QTextStream stream(&file);
        stream.setCodec("UTF-8");
    
    • 1
    • 2
    • 3
    • 4

    将文本框就设置为只读属性

    	m_textEdit = new QTextEdit(this);
        m_textEdit->setReadOnly(true);  //将文本设置为只读属性,不然现实的文本框居然还能够被修改。
    
    • 1
    • 2

    对于文本内数据的操作

    	//过滤首行
        if(nLines == 0){
            stream.readLine();
            nLines++;
            continue;
        }
    
    	//读取一行
    	strText += stream.readLine();
    	strText += "\n";
    	nLines++;
    	
    	//处理答案的操作(可放在循环里面)
    	strLine = stream.readLine();    //先读取这一行
        strList = strLine.split(" ");   //对答案进行处理——以空格进行分割(eg:答案 A)
        m_answerList.append(strList.at(1));//把答案存放到答案链表里边 因为0:答案 1:A\b\c\d
        strText += "\n";
        nLines++;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    实现窗口显示考试时间

    setWindowTitle("考试已用时:0分0秒");
    
    m_timer = new QTimer(this);
    m_timer->setInterval(1000);	//1s钟一次
    m_timer->start();
    
    connect(m_timer,SIGNAL(timeout()),this,SLOT(freshTime()));
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    下篇预告

    驾校科目一考试系统——布局按钮
    
    • 1
  • 相关阅读:
    代码随想录刷题笔记 DAY 37 | 动态规划理论基础 | 斐波那契数 No.509 | 爬楼梯 No.70 | 使用最小花费爬楼梯 No.746
    项目十一文件的应用
    玩转输入输出
    解决pip安装包后但是Pycharm检测不到
    8.并发编程之Automic&Unsafe魔法类详解
    FineReport智能表格软件-JS实现大数据集导出(一)
    VSCode不同窗口设置不同颜色
    Servlet【方法使用】
    65.AutoML-1
    【企业动态】欢迎法国客户来访东胜物联,深入探讨智能化合作
  • 原文地址:https://blog.csdn.net/dearQiHao/article/details/125400852