写在前面:
一直想做一个可以辅助期末复习的东西。作为计算机专业的学生,如果单说敲代码的话那问题不是很大,但是涉及到概念之类的题就会让我很头疼,所以打算做一个可以用于备战期末刷概念题的东西。很好,在期末之前写好了。在此记录一波。
准备工作:
下载Qt:Qt安装教程
开始:
头文件:
c语言的头文件格式:
#ifndef 头文件名(大写字母,其中的'.'要换成'_')
#define 头文件名(大写字母,其中的'.'要换成'_')
/*..........
中间是内容
..........
*/
#endif
工具头文件:
作用:在这个头文件里放头文件,后面的文件直接应用这个工具头文件就行了,这样很方便。
#ifndef TOOL_H
#define TOOL_H
#include <QWidget>
#include<QMainWindow>
#include<QDialog>
#include<QApplication>
#include <QSqlDatabase>
#include <QMessageBox>
#include <QDebug>
#include <QSqlError>
#include <QSqlQuery>
#include<QPushButton>
#include<QLayout>
#include<string>
#include<iostream>
#include<QLabel>
#include<QPixmap>
#include<QLayout>
#include<QString>
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QTableView>
#include <QtWidgets/QWidget>
#include <QWidget>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSqlError>
#include <QDebug>
#include <QMessageBox>
#include <QSqlTableModel>
#include<tableshow.h>
#include<ctime>
#include<QEventLoop>
#include<QTimer>
#include<vector>
#include<cstring>
#include<QPalette>
#include<QFont>
#include<QSqlRecord>
#include<windows.h>
#endif // TOOL_H
主文件:
main.cpp
一般情况下,主文件里都是这样,一个main函数。
我们程序的重点在于其他的类中
#include "widget.h"
#include <QApplication>
#include<QSqlDatabase>
#include<QString>
#include<string>
#include<iostream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
主要的类文件:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include<createrwin.h>
#include"tool.h"
#include<res.h>
#include"xzwin.h"
#include"tkwin.h"
#include<ch2.h>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
void showWindow();
void tocreatecin();
~Widget();
};
class Window : public QMainWindow
{
Q_OBJECT
public:
Window(QMainWindow *parent = nullptr);
void showDialog1();
void showDialog2();
void showWidget();
~Window();
};
class Dialog : public QDialog
{
Q_OBJECT
public:
int thelong;
Dialog(QDialog *parent = nullptr);
void showDialogFirst();
void showDialogSecond();
void showWindow();
void showques1();//操作系统题目展示逻辑
void showques2();//计算机组成题目展示逻辑
void openxzwin1(int id);//打开操作系统选择题页面
void openxzwin2(int id);//打开计组选择题页面
~Dialog();
void creatob();//连接数据库
private:
QSqlDatabase db;//建立qt与mysql连接
QSqlQueryModel model;//保存结果集
QSqlTableModel* Tmodel;
};
#endif // WIDGET_H
widget.cpp
写这个文件的时候我还不熟悉qt的一些操作,所以在cpp里面实现界面
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->resize(400,300);
this->setWindowTitle("开始页面");
QPushButton *chose1 = new QPushButton("开始挑战",this);//选择按钮
QPushButton *exi = new QPushButton("退出",this);//关闭按钮
QLabel *wel = new QLabel(this);
QPixmap pn;
pn.load(":/new/prefix1/welcome.png");
wel->setPixmap(pn);
wel->resize(400,200);
wel->setScaledContents(true);
chose1->resize(150,50);
exi->resize(150,50);
chose1->move(20,230);
exi->move(220,230);
chose1->show();
exi->show();
//Window *a = new Window;
//on_pushButton_clicked();
connect(exi,&QPushButton::clicked,this,&QWidget::close);
connect(chose1,&QPushButton::clicked,this,&Widget::showWindow);
QPushButton *cmd = new QPushButton("管理者选项",this);
cmd->resize(100,30);
cmd->move(300,200);
cmd->show();
connect(cmd,&QPushButton::clicked,this,&Widget::tocreatecin);
}
Window::Window(QMainWindow *parent)
: QMainWindow(parent)
{
this->resize(400,200);
this->setWindowTitle("选择题库");
QPushButton *btn1 = new QPushButton("操作系统",this);
QPushButton *btn2 = new QPushButton("计算机组成",this);
QPushButton *btn3 = new QPushButton("上一页",this);
btn1->resize(150,150);
btn2->resize(150,150);
btn3->resize(100,25);
btn3->move(0,0);
btn3->show();
btn1->move(25,25);
btn2->move(225,25);
connect(btn1,&QPushButton::clicked,this,&Window::showDialog1);
connect(btn2,&QPushButton::clicked,this,&Window::showDialog2);
connect(btn3,&QPushButton::clicked,this,&Window::showWidget);
}
Dialog::Dialog(QDialog *parent)
:QDialog(parent)
{
}
//Widget的函数列
void Widget::showWindow()
{
Window *a = new Window;
this->close();
a->show();
}
void Widget::tocreatecin()
{
createrWin *a = new createrWin;
a->show();
this->close();
}
//Window的函数列
void Window::showDialog1()
{
ch2 *a = new ch2;
this->close();
a->flag = 1;
a->show();
}
void Window::showDialog2()
{
ch2 *a = new ch2;
this->close();
a->flag = 2;
a->show();
}
void Window::showWidget()
{
Widget *a = new Widget;
this->close();
a->show();
}
//Dialog的函数列
void Dialog::showDialogFirst()
{
this->resize(400,200);
this->setWindowTitle("操作系统题库,确定?");
QPushButton *btn1 = new QPushButton("确定",this);
QPushButton *btn2 = new QPushButton("算了算了",this);
btn1->resize(150,150);
btn2->resize(150,150);
btn1->move(25,25);
btn2->move(225,25);
btn1->show();
btn2->show();
connect(btn1,&QPushButton::clicked,this,&Dialog::showques1);
connect(btn2,&QPushButton::clicked,this,&Dialog::showWindow);
}
void Dialog::showDialogSecond()
{
this->resize(400,200);
this->setWindowTitle("计算机组成题库,确定?");
QPushButton *btn1 = new QPushButton("确定",this);
QPushButton *btn2 = new QPushButton("算了算了",this);
btn1->resize(150,150);
btn2->resize(150,150);
btn1->move(25,25);
btn2->move(225,25);
btn1->show();
btn2->show();
connect(btn1,&QPushButton::clicked,this,&Dialog::showques2);
connect(btn2,&QPushButton::clicked,this,&Dialog::showWindow);
}
void Dialog::showWindow()
{
Window *a = new Window;
this->close();
a->show();
}
void Dialog::showques1()
{
xzwin *a = new xzwin;//建立选择题窗口
tkwin *b = new tkwin;//建立判断题窗口
res *n = new res;//结果窗口
a->flag = 1;
b->flag = 1;
n->flag = 1;
a->score = 0;
b->score = 0;
std::vector<int>theone;
std::vector<int>thetwo;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("mytiku.db");
if(db.open())
qDebug()<<"数据库连接成功\n";
else
qDebug()<<"连接失败\n";
int thenum;
QSqlTableModel *t1 = new QSqlTableModel(this,db);
t1->setTable("linuxchooseque");
t1->select();
int th1 = t1->rowCount();
QSqlTableModel *t2 = new QSqlTableModel(this,db);
t2->setTable("linuxthinkque");
t2->select();
int th2 = t2->rowCount();
thenum = th1+th2;
qDebug()<<"计算机组成原理选择题题数:"<<th1<<'\n';
qDebug()<<"计算机组成原理判断题题数:"<<th2<<'\n';
qDebug()<<"总题数"<<thenum<<'\n';
qsrand(time(NULL));//取随机数
for(int i=1;i<=thelong;i++)
{
this->close();
int id = qrand()%thenum;
//openxzwin2(id+1);
if(id>th1)
{
b->isused = true;
b->getdata(id-th1);
b->tid = id-th1;
b->totitle(i);
b->show();//判断题窗口出题条件
b->totodo();
//b->close();
b->toclear();
}
else
{
a->isused = true;
a->getdata(id+1);
a->tid = id+1;
a->totitle(i);
a->show();
//this->close();
a->totodo();
//a->close();
a->toclear();
}
qDebug()<<"选择题得分:"<<a->score<<'\n';
qDebug()<<"判断题得分:"<<b->score<<'\n';
n->toscore(a->score,b->score);
}
theone = a->noque;
thetwo = b->noque;
n->tonote(theone,thetwo);
n->show();
a->close();
b->close();
}
void Dialog::showques2()
{
xzwin *a = new xzwin;//建立选择题窗口
tkwin *b = new tkwin;//建立判断题窗口
res *n = new res;//结果窗口
a->flag = 2;
b->flag = 2;
n->flag = 2;
a->score = 0;
b->score = 0;
std::vector<int>theone;
std::vector<int>thetwo;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("mytiku.db");
if(db.open())
qDebug()<<"数据库连接成功\n";
else
qDebug()<<"连接失败\n";
int thenum;
QSqlTableModel *t1 = new QSqlTableModel(this,db);
t1->setTable("jzchooseque");
t1->select();
int th1 = t1->rowCount();
QSqlTableModel *t2 = new QSqlTableModel(this,db);
t2->setTable("jzthinkque");
t2->select();
int th2 = t2->rowCount();
thenum = th1+th2;
qDebug()<<"计算机组成原理选择题题数:"<<th1<<'\n';
qDebug()<<"计算机组成原理判断题题数:"<<th2<<'\n';
qDebug()<<"总题数"<<thenum<<'\n';
int thepos=0;
qsrand(time(NULL));//取随机数
for(int i=1;i<=thelong;i++)
{
bool wanc = false;
int id = qrand()%thenum;
//openxzwin2(id+1);
if(id>th1)
{
b->isused = true;
b->getdata(id-th1);
b->tid = id-th1;
b->totitle(i);
QString str = QString("当前进度: %1/%2").arg(i).arg(thelong);
b->setta(str);
b->show();//判断题窗口出题条件
this->close();
b->totodo();
// if(b->isused)
// {
// QMessageBox::critical(this,"警告","你还没有点确定");
// }
wanc = b->isclicked;
b->toclear();
}
else
{
a->isused = true;
a->isclicked = false;
a->getdata(id+1);
a->tid = id+1;
a->totitle(i);
QString str = QString("当前进度: %1/%2").arg(i).arg(thelong);
a->setta(str);
a->show();
this->close();
a->totodo();
// if(a->isused)
// {
// QMessageBox::critical(this,"警告","你还没有点确定");
// }
a->toclear();
wanc = a->isclicked;
}
qDebug()<<"选择题得分:"<<a->score<<'\n';
qDebug()<<"判断题得分:"<<b->score<<'\n';
n->toscore(a->score,b->score);
thepos++;
if(!wanc)break;
}
theone = a->noque;
thetwo = b->noque;
n->tonote(theone,thetwo);
if(thepos == thelong)
n->show();
a->close();
b->close();
}
void Dialog::openxzwin1(int id)
{
xzwin *a = new xzwin;
a->flag = 1;
a->getdata(id);
a->show();
this->close();
}
void Dialog::openxzwin2(int id)
{
xzwin *a = new xzwin;
a->flag = 2;
a->getdata(id);
a->show();
this->close();
}
//析构函数列
Widget::~Widget()
{
delete this;
}
Window::~Window()
{
delete this;
}
Dialog::~Dialog()
{
delete this;
}
void Dialog::creatob()
{
//代码
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("mytiku.db");
if(db.open())
qDebug()<<"数据库连接成功\n";
else
qDebug()<<"连接失败\n";
}
以下为效果图:
这是Widget页面:
这是Window界面:
这是Dialog界面:
这个确实很简陋,因为是cpp操纵,所以这么简陋的界面也花了我不少时间。
还好,后面学了qt设计师界面类操作,方便了许多。
还有些内容,欠着等会儿写
遇到的问题以及解决办法
1,界面跳转:
由于这个玩意本身占的内存小,且逻辑简单,所以我用了一种可能会有内存问题的方法解决。
我是在类里面写一个函数,将按钮与这个函数连接,如果点击按钮,那么就像以下这样
connect(btn1,&QPushButton::clicked,this,&类名::clicked);
void 类名::clicked1()
{
Widget *a = new Widget;
this->close();
a->show();
return;
}
这样便可以实现页面的跳转
2,数据库连接
我这里在连接MySql的时候出了很多问题,最后摆烂,选择用Sqlite
这个倒好,简单得不得了,如下面:
class 类:
public:
//一大堆函数......
void creatob();//连接数据库
private:
QSqlDatabase db;//建立qt与mysql连接
QSqlQueryModel model;//保存结果集
QSqlTableModel* Tmodel;
void 类::creatob()
{
//代码
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("文件名.db");
if(db.open())
qDebug()<<"数据库连接成功\n";
else
qDebug()<<"连接失败\n";
}
好了,就连接成功了~
拖会儿,剩下的晚上再写
成品:
我用夸克网盘分享了「计组与操作系统(修正版).zip」,点击链接即可保存。打开「夸克APP」,无需下载在线播放视频,畅享原画5倍速,支持电视投屏。
链接:https://pan.quark.cn/s/ae9851553625
提取码:RRwN