Qt驾校科目源码 基本都有标注
作者:互联网
验证题目是否全选没有写!!!哈哈哈
examdialogs.h 头文件
#ifndef EXAMDIALOGS_H
#define EXAMDIALOGS_H
#include <QDialog>
#include <QTimer> //时间
#include <QRadioButton> //单选按钮
#include <QCheckBox> //多选按钮
#include <QLabel> //标签
#include <QTextEdit> //文本对话框
#include <QGridLayout> //布局管理器
#include <QTextStream> // 文本流
#include <QFile> // 打开文件
#include <QMessageBox> //对话框
#include <QButtonGroup>
#include <QPushButton> //按钮
class ExamDialogs : public QDialog
{
Q_OBJECT
public:
ExamDialogs(QWidget *parent = 0);
void initTimer(); //时间初始化
void initLayou(); //初始话布局管理器
void initButton(); //初始化按钮和标签
bool initText(); //初始化文本
private:
QTimer *m_timer;
int m_timerGo;
QLabel *m_title[10]; //标签
QRadioButton *m_radioBtn[32]; //单项选择
QButtonGroup *m_btnGroup[9]; //按钮分组
QCheckBox *m_CheckBtnl[4]; //多项选择
QTextEdit *m_textEditl; //文本
QRadioButton *m_radioA; //正确
QRadioButton *m_radioB; //错误
QStringList m_answerList; //答案
QGridLayout *m_layout; //布局管理器
private slots:
void freshTimer();
void getscore();
};
#endif // EXAMDIALOGS_H
examdialogs.cpp 文件
#include "examdialogs.h"
#include <QApplication>
ExamDialogs::ExamDialogs(QWidget *parent):QDialog(parent)
{
this->setFixedSize(800,600);
this->setWindowTitle("考试时间0分0秒");
QFont fon;
fon.setPointSize(10); //设置字体字号
setFont(fon); //设置字体
initTimer(); //初始化时间
initLayou(); //初始化布局管理器
initButton(); // 初始化按钮
if (!initText()){
QMessageBox::information(this,"提示","数据文件读取失败");
QMessageBox::information(this,"提示","初始化数据库文件失败");
QTimer::singleShot(0,qApp,SLOT(quit()));
}
}
void ExamDialogs::initTimer()
{
m_timer = new QTimer(this);
m_timerGo =0;
m_timer->setInterval(1000); // 设置刷新时间
m_timer->start(); //启动
//槽方法: 信号与槽联系起来
//
connect(m_timer,SIGNAL(timeout()),this,SLOT(freshTimer()));
}
void ExamDialogs::initLayou() // 初始化布局管理器
{
m_layout = new QGridLayout(this);
m_layout->setSpacing(10); //设置控件间隙
m_layout->setMargin(10); // 设窗体与控件的间隙
}
void ExamDialogs::initButton()
{
QStringList textlist = {"A","B","C","D"};
for(int i=0;i< 10;i++){
m_title[i] = new QLabel(this);
m_title[i]->setText("第" + QString::number(i +1) + "题"); //设置文本标题
m_layout->addWidget(m_title[i],1,i); //添加部件
if(i < 8) m_btnGroup[i] = new QButtonGroup(this);
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_btnGroup[8] = new QButtonGroup(this);
m_btnGroup[8]->addButton(m_radioA); //按钮分组 添加按钮
m_btnGroup[8] ->addButton(m_radioB); // 按钮分组 添加按钮
break;
}
for(int j=0;j < 4;j++){
if(i ==8){
m_CheckBtnl[j] = new QCheckBox(this);
m_CheckBtnl[j]->setText(textlist.at(j)); //设置文本
m_layout->addWidget(m_CheckBtnl[j],2+j,8); //添加部件
}else{
m_radioBtn[4 * i +j] = new QRadioButton(this);
m_radioBtn[4 * i + j] ->setText(textlist.at(j)); //设置文本
m_layout->addWidget( m_radioBtn[4* i+ j],2+j,i); //添加部件
m_btnGroup[i] ->addButton(m_radioBtn[4 * i +j]); //添加部件
}
}
}
QPushButton *button = new QPushButton(this);
button->setFixedSize(70,30); //固定按钮大小
button->setText("提交"); //设置文本
m_layout->addWidget(button,5, 9); //添加部件
connect(button,SIGNAL(clicked(bool)),this,SLOT(getscore())); // 槽方法 获取分数
}
bool ExamDialogs::initText()
{
QString strLine; //答案行 文本
QStringList strlist; //答案
QString filename("exam.txt");
QFile file(filename);
QTextStream stream(&file);
stream.setCodec("UTF-8");
int nLines = 0;
if(file.open(QIODevice::ReadOnly | QIODevice :: Text)){ // 只读 纯文本
m_textEditl = new QTextEdit(this);
m_textEditl->setReadOnly(true); //文本框内 只读不可编辑
QString strtText;
while(!stream.atEnd()){
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)); //答案添加到 链表
strtText +="\n";
nLines++;
continue;
}
strtText += stream.readLine();
nLines++;
strtText +="\n";
}
m_textEditl->setText(strtText); //设置文本
m_layout->addWidget(m_textEditl,0,0,1,10); //添加部件
return true;
}else{
file.close();
return false;
}
}
void ExamDialogs::freshTimer()
{
m_timerGo++; // 时间+1
QString min =QString::number(m_timerGo / 60); //分
QString sec = QString::number(m_timerGo % 60); //秒
QString res = "考试时间" + min + "分" + sec + "秒";
this->setWindowTitle(res); //设置标题时间
}
void ExamDialogs::getscore() //获取分数
{
int score = 0;
for(int i=0; i < 10;i++){
if(i < 8)
if(m_btnGroup[i]->checkedButton()->text() == m_answerList.at(i)){
score +=10;
}
if(i == 8){
QString accounts = m_answerList.at(i);
bool hasA = false;
bool hasB = false;
bool hasC = false;
bool hasD = false;
if(accounts.contains("A")) hasA = true;
if(accounts.contains("B")) hasB = true;
if(accounts.contains("C")) hasC = true;
if(accounts.contains("D")) hasD = true;
bool checkA = m_CheckBtnl[0]->checkState();
bool checkB = m_CheckBtnl[1]->checkState();
bool checkC = m_CheckBtnl[2]->checkState();
bool checkD = m_CheckBtnl[3]->checkState();
if(hasA != checkA) continue;
if(hasB != checkB) continue;
if(hasC != checkC) continue;
if(hasD != checkD) continue;
score+=10;
}
if(i==9){
if(m_btnGroup[8]->checkedButton()->text() == m_answerList.at(i))
score+=10;
}
}
QString str = "您的分数是:" + QString::number(score) + "分,是否重新考试?";
int res = QMessageBox::information(this,"提示",str,QMessageBox::Yes | QMessageBox::No);
if(res == QMessageBox::Yes)
return;
else
close();
}
标签:layout,Qt,void,源码,bool,QString,new,驾校,include 来源: https://blog.csdn.net/NuoXHS/article/details/112451107