前言
本文章主要讲解本人在QT学习期间所开发的项目-学生管理系统,代码主要参考于网上查找。
一、界面展示
功能主要包括,学生信息的插入删除,以及修改。
再加上按照id,或者成绩的升降序排序
二、代码展示
1.pro
QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = 12
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has 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 \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
2.widget.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
3. main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
StudentDialog w;
w.show();
return a.exec();
}
4.widget.cpp
#include "widget.h".h"
#include "ui_widget.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();
}
5.widget.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>
总结文章来源:https://www.toymoban.com/news/detail-530614.html
项目处于学习阶段所做,参考了一些网络上的代码和思路,且功能简单,仅做练习使用。文章来源地址https://www.toymoban.com/news/detail-530614.html
到了这里,关于QT项目-学生管理系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!