今天带来一个Qt的小项目 学生管理系统
程序运行如下:
这个是图形化的操作,应该是一个简单的小项目,可以复习一下Qt数据库的知识
这个程序包括排序 插入数据等功能
Stu.pro:
```xml
#-------------------------------------------------
#
# Project created by QtCreator 2021-04-14T15:21:02
#
#-------------------------------------------------
QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Stu
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
studentdialog.cpp
HEADERS += \
studentdialog.h
FORMS += \
studentdialog.ui
studentdialog.h:
#ifndef STUDENTDIALOG_H
#define STUDENTDIALOG_H
#include <QDialog>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSqlError>
#include <QDebug>
#include <QMessageBox>
namespace Ui {
class StudentDialog;
}
class StudentDialog : public QDialog
{
Q_OBJECT
public:
explicit StudentDialog(QWidget *parent = 0);
~StudentDialog();
private:
//创建数据库
void createDB();
//创建数据表
void createTable();
//查询
void queryTable();
private slots:
//插入
void on_InsertpushButton_clicked();
//删除
void on_DelpushButton_clicked();
//修改
void on_UpdatepushButton_clicked();
//排序按钮
void on_sortpushButton_clicked();
//清空
void CleanEdit();
private:
Ui::StudentDialog *ui;
//建立和数据库的连接
QSqlDatabase db;
//保存结果集
QSqlQueryModel model;
};
#endif // STUDENTDIALOG_H
studentdialog.cpp:
#include "studentdialog.h"
#include "ui_studentdialog.h"
#pragma execution_character_set("utf-8")
StudentDialog::StudentDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::StudentDialog)
{
ui->setupUi(this);
this->setWindowTitle("学生管理系统");
createDB();
createTable();
queryTable();
}
StudentDialog::~StudentDialog()
{
delete ui;
}
//插入操作
void StudentDialog::on_InsertpushButton_clicked()
{
QSqlQuery query;
int id=ui->IDEdit->text().toInt();
QString name=ui->NameEdit->text();
double score=ui->ScoreEdit->text().toDouble();
if(id==0){
QMessageBox::critical(this,"Error","id ERROR");
CleanEdit();
return;
}
if(name==""){
QMessageBox::critical(this,"Error","name ERROR");
CleanEdit();
return;
}
if(score<=0||score>150){
QMessageBox::critical(this,"Error","score ERROR");
CleanEdit();
return;
}
//类似C语言%d被替换
QString str=QString("INSERT INTO student (id,name,score) VALUES(%1,'%2',%3)"
).arg(id).arg(name).arg(score);
if(query.exec(str)==false){
qDebug()<<str;
}else{
CleanEdit();
qDebug()<<"insert success";
queryTable();
}
}
//删除操作 根据ID
void StudentDialog::on_DelpushButton_clicked()
{
QSqlQuery query;
int id=ui->IDEdit->text().toInt();
QString str=QString("DELETE FROM student WHERE id = %1").arg(id);
if(QMessageBox::question(this,"DELETE","ARE YOU SURE?",QMessageBox::Yes|QMessageBox::No)
==QMessageBox::No){
return;
}
if(query.exec(str)==false){
qDebug()<<str;
}else{
CleanEdit();
qDebug()<<"Delete success";
queryTable();
}
}
//修改操作 根据ID
void StudentDialog::on_UpdatepushButton_clicked()
{
QSqlQuery query;
int id=ui->IDEdit->text().toInt();
double score=ui->ScoreEdit->text().toDouble();
QString str=QString("UPDATE student SET score=%1 WHERE id=%2").arg(score).arg(id);
if(query.exec(str)==false){
qDebug()<<str;
}else{
CleanEdit();
qDebug()<<"Update success";
queryTable();
}
}
//排序按钮
void StudentDialog::on_sortpushButton_clicked()
{
//获取排序列名
QString value=ui->ValueComboBox->currentText();
//获取排序方式名字
QString condition;
if(ui->condComboBox->currentIndex()==0){
condition="ASC";
}else{
condition="DESC";
}
QString str=QString("SELECT * FROM student ORDER BY %1 %2").arg(value).arg(condition);
//查询显示
model.setQuery(str);
ui->tableView->setModel(&model);
}
//创建数据库
void StudentDialog::createDB(){
//添加数据库的驱动
db=QSqlDatabase::addDatabase("QSQLITE");
//设置数据库名字
db.setDatabaseName("student.db");
//打开数据库
if(db.open()==true){
qDebug()<<"create darabase success!";
}else{
qDebug()<<"create darabase fail!";
}
}
//创建数据表
void StudentDialog::createTable(){
QSqlQuery query;
QString str=QString("CREATE TABLE student ("
"id INT PRIMARY KEY NOT NULL,"
"name TEXT NOT NULL,"
"score REAL NOT NULL)");
if(query.exec(str)==false){
qDebug()<<str;
qDebug()<<"fail";
}else{
qDebug()<<"success";
}
}
//查询
void StudentDialog::queryTable(){
QString str=QString("SELECT * FROM student");
model.setQuery(str);
ui->tableView->setModel(&model);
}
void StudentDialog::CleanEdit(){
ui->IDEdit->clear();
ui->NameEdit->clear();
ui->ScoreEdit->clear();
}
studentdialog.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>StudentDialog</class>
<widget class="QDialog" name="StudentDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>523</width>
<height>509</height>
</rect>
</property>
<property name="font">
<font>
<family>Agency FB</family>
<pointsize>10</pointsize>
</font>
</property>
<property name="windowTitle">
<string>StudentDialog</string>
</property>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>7</y>
<width>511</width>
<height>491</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QPushButton" name="sortpushButton">
<property name="text">
<string>排序</string>
</property>
</widget>
<widget class="QComboBox" name="condComboBox">
<item>
<property name="text">
<string>升序</string>
</property>
</item>
<item>
<property name="text">
<string>降序</string>
</property>
</item>
</widget>
<widget class="QComboBox" name="ValueComboBox">
<property name="font">
<font>
<family>Agency FB</family>
<pointsize>10</pointsize>
</font>
</property>
<item>
<property name="text">
<string>ID</string>
</property>
</item>
<item>
<property name="text">
<string>Score</string>
</property>
</item>
</widget>
</widget>
</item>
<item>
<widget class="QTableView" name="tableView"/>
</item>
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="font">
<font>
<family>Agency FB</family>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>学号:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="IDEdit"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>姓名:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="NameEdit"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>成绩:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="ScoreEdit"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="InsertpushButton">
<property name="text">
<string>插入</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="DelpushButton">
<property name="text">
<string>删除</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="UpdatepushButton">
<property name="text">
<string>修改</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
这就是本次项目的全部代码,完全开源,可以复习复习Qt数据库的知识