2023-12-05 Qt学习总结10


点击 <C 语言编程核心突破> 快速C语言入门


Qt学习总结

  • 前言
  • 二十六 学生信息管理系统
      • 插入介绍: `QTableView`和`QSqlTableModel`
        • QTableView
        • QSqlTableModel
      • 程序所用数据库表格
      • 程序组成以及界面
      • 学生端源码:
      • 管理员端源码:
  • 总结


前言

要解决问题: 学习qt最核心知识, 多一个都不学.


二十六 学生信息管理系统

用Qt实现一个含界面的简单学生信息管理系统, 主要功能:

  1. 登录和注册功能:系统需要有注册和登录功能,学生可以通过注册账号并登录来使用系统。

  2. 学生信息录入:系统需要支持学生信息的录入和管理,包括学生姓名、学号、性别、年龄、联系方式等信息。

  3. 学生信息查询:系统需要支持信息查询功能,学生可以通过查询功能查看自己的个人信息。

  4. 学生信息修改:学生可以随时修改自己的个人信息,如联系方式。

  5. 学生成绩管理:系统需要支持学生成绩信息的录入和管理,包括学生课程成绩等信息。

  6. 学生成绩查询:学生可以通过系统查询自己的课程成绩。

  7. 班级信息管理:系统需要支持班级信息的录入和管理,包括班级名称、班级简介等信息。

  8. 公告管理:系统需要支持公告管理功能,管理员可以通过系统发布学校或班级的公告通知。

  9. 系统安全:系统需要有安全保障措施,防止信息泄露或被恶意攻击。

  10. 界面友好:系统需要具备友好的用户界面,方便学生和管理员使用。

在这里插入图片描述

插入介绍: QTableViewQSqlTableModel

QTableView

QTableView是Qt框架中的一个控件,用于显示和编辑过滤表格数据。

它是一个基于模型/视图架构的控件,通过使用模型来管理数据,视图来显示数据。

以下是使用QTableView的一些常见方法和C++函数:

  1. 设置模型

可以通过setModel()函数将一个QAbstractTableModel类型的模型设置给QTableView控件。

模型通过继承QAbstractTableModel类实现,它负责管理数据。

常用的模型类有QStandardItemModelQSqlTableModel

  1. 设置选择模式

通过setSelectionBehavior()setSelectionMode()函数设置选择模式,用于指定用户如何在表格中选择行或单元格。

其中,setSelectionBehavior()函数指定用户选择行或单元格时的行为,而setSelectionMode()函数指定用户可以选择的行数。

  1. 设置表头

通过setHorizontalHeader()setVerticalHeader()函数设置自定义表头。

  1. 设置单元格属性

可以通过setItemDelegate()函数设置一个委托类,以控制QTableView中每个单元格的显示和编辑行为.

  1. 隐藏列

通过hideColumn()函数隐藏某一列.

QSqlTableModel

QSqlTableModel是Qt提供的一种数据库模型类,用于提供对单表数据的读写、修改、删除等操作。它继承于QAbstractTableModel,可以在TableView中使用。

使用方法:

  1. 首先需要创建数据库连接

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("myDB.sqlite");
    if(!db.open())
    {qDebug()<<"open database error!";return ;
    }
    
  2. 创建QSqlTableModel对象

    QSqlTableModel *model = new QSqlTableModel(this, db); // this表示当前类是model的父类,对象被删除时会自动删除model对象
    model->setTable("student");
    model->setSort(0, Qt::AscendingOrder); // 按照某一列升序排序
    model->select(); // 获取所有数据
    
  3. TableView中显示数据

    ui->tableView->setModel(model);
    
  4. 更新表格中第一行的数据:

    QModelIndex index = model->index(0, 1); // 获取第一行第二列的index
    model->setData(index, "NewValue", Qt::EditRole); // 设置新值
    model->submitAll(); // 提交更改
    

常用函数

  1. setTable(const QString &tableName):设置表名

  2. setFilter(const QString &filter):设置筛选条件

  3. setSort(int column, Qt::SortOrder order):设置排序

  4. select():从表中获取数据

  5. setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole):更新数据

  6. insertRows(int row, int count, const QModelIndex &parent = QModelIndex()):插入行数据

  7. removeRows(int row, int count, const QModelIndex &parent = QModelIndex()):删除行数据

  8. setEditStrategy(EditStrategy strategy):置修改策略,决定了如何在模型中更改数据,并决定在什么条件下提交更改到数据库。 SqlTableModel提供了三种修改策略:QSqlTableModel::OnFieldChange(默认值)当编辑器中的一个单元格的内容改变时,自动提交该单元格的内容。 QSqlTableModel::OnRowChange 当编辑器中的一整行的内容改变时,自动提交该行的内容。 QSqlTableModel::OnManualSubmit 不自动提交更改,需要调用submitAll()函数手动提交。

程序所用数据库表格

需要三个表:

  1. login 这个给管理员使用, 校对管理员密码, 我们这里设置默认用户名和密码都是root, 可以直接在系统下改
CREATE TABLE login(name varchar(16) NOT NULL,stuPassword varchar(16) NOT NULL
);
  1. news 这个是储存班级和学校通知
CREATE TABLE news(theDate date NOT NULL,theTitle varchar(256) NOT NULL,theArtical varchar(2048) NOT NULL
);
  1. student 这个是储存学生信息, 包括学号, 登录密码, 学生信息, 学生成绩
CREATE TABLE student(stuNumber integer UNSIGNED PRIMARY KEY,stuPassword varchar(16) NOT NULL,stuName varchar(16) NOT NULL,sex varchar(8) NOT NULL,birth date NOT NULL,phoneNumber varchar(16),math integer UNSIGNED,chinese integer UNSIGNED,english integer UNSIGNED,physical integer UNSIGNED,chemistry integer UNSIGNED
);

程序组成以及界面

分为学生客户端和管理员端, 使用两个独立程序, 学生端有三个界面, 一个是登录, 另一个是注册, 还有查询.
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

管理端, 有注册, 成绩录入和信息推送.
在这里插入图片描述
在这里插入图片描述

学生端源码:

reg.h

#ifndef REG_H
#define REG_H#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QWidget>namespace Ui
{
class reg;
}class reg : public QWidget
{Q_OBJECTpublic:explicit reg(int stuNum_, QString passNumber_, QSqlDatabase *qdata_,QWidget *parent = nullptr);~reg();private slots:void on_cancelButton_clicked();void on_OkButton_clicked();private:Ui::reg *ui;unsigned int stuNum;QString passNumber;QSqlDatabase *qdata;
};#endif // REG_H

student.h

#ifndef STUDENT_H
#define STUDENT_H#include <QSqlDatabase>
#include <QSqlQuery>
#include <QWidget>namespace Ui
{
class student;
}class student : public QWidget
{Q_OBJECTpublic:explicit student(unsigned int stuNum_, QSqlDatabase *qdata_,QWidget *parent = nullptr);~student();private slots:void on_stuInfButton_clicked();void on_phoneChangeButton_clicked();void on_scoreButton_clicked();void on_newsButton_clicked();private:Ui::student *ui;unsigned int stuNum;QSqlDatabase *qdata;
};#endif // STUDENT_H

Widget.h

#ifndef WIDGET_H
#define WIDGET_H#include "reg.h"
#include "student.h"
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QWidget>QT_BEGIN_NAMESPACE
namespace Ui
{
class Widget;
}
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_loginButton_clicked();void on_registButton_clicked();private:Ui::Widget *ui;QSqlDatabase qdata;student *stu = nullptr;reg *regist = nullptr;
};
#endif // WIDGET_H

main.cpp

#include "Widget.h"#include <QApplication>
#include <QLocale>
#include <QTranslator>int main(int argc, char *argv[])
{QApplication a(argc, argv);QTranslator translator;const QStringList uiLanguages = QLocale::system().uiLanguages();for (const QString &locale : uiLanguages) {const QString baseName = "Learn_24_" + QLocale(locale).name();if (translator.load(":/i18n/" + baseName)) {a.installTranslator(&translator);break;}}Widget w;w.show();return a.exec();
}

reg.cpp

#include "reg.h"
#include "ui_reg.h"reg::reg(int stuNum_, QString passNumber_, QSqlDatabase *qdata_,QWidget *parent): QWidget(parent), ui(new Ui::reg), stuNum(stuNum_), passNumber(passNumber_), qdata(qdata_)
{ui->setupUi(this);ui->stuNumberLineEdit->setText(QString::number(stuNum));
}reg::~reg()
{qDebug() << "delete reg";delete ui;
}void reg::on_cancelButton_clicked()
{this->close();
}void reg::on_OkButton_clicked()
{QSqlQuery qsql(*qdata);if (ui->stuNameLineEdit->text().isEmpty() ||ui->sexLineEdit->text().isEmpty() ||ui->birthdayLineEdit->text().isEmpty()){QMessageBox::information(this, "提示", "姓名, 性别, 年龄不可为空");}else{if (qsql.exec(QString("INSERT INTO student VALUES ('%1', '%2', '%3', '%4', '%5', ""'%6', '', '', '', '', '');").arg(QString::number(stuNum)).arg(passNumber).arg(ui->stuNameLineEdit->text()).arg(ui->sexLineEdit->text()).arg(ui->birthdayLineEdit->text()).arg(ui->phoneLineEdit->text()))){qDebug() << "OK";}}
}

student.cpp

#include "student.h"
#include "ui_student.h"student::student(unsigned int stuNum_, QSqlDatabase *qdata_, QWidget *parent): QWidget(parent), ui(new Ui::student), stuNum(stuNum_), qdata(qdata_)
{ui->setupUi(this);QSqlQuery qsql(*qdata);if (qsql.exec(QString("SELECT * FROM student WHERE stuNumber = %1;").arg(stuNum))){qsql.next();ui->numberLineEdit->setText(qsql.value(0).toString());ui->nameLineEdit->setText(qsql.value(2).toString());ui->sexLineEdit->setText(qsql.value(3).toString());ui->birthdayLineEdit->setText(qsql.value(4).toString());ui->phoneLineEdit->setText(qsql.value(5).toString());}
}student::~student()
{qDebug() << "delete student";delete ui;
}void student::on_stuInfButton_clicked()
{QSqlQuery qsql(*qdata);if (qsql.exec(QString("SELECT * FROM student WHERE stuNumber = %1;").arg(stuNum))){qsql.next();ui->phoneLineEdit->setText(qsql.value(5).toString());}
}void student::on_phoneChangeButton_clicked()
{QSqlQuery qsql(*qdata);if (qsql.exec(QString("UPDATE student SET phoneNumber = '%1' WHERE stuNumber = '%2';").arg(ui->phoneLineEdit->text()).arg(stuNum))){qDebug() << "OK";}
}void student::on_scoreButton_clicked()
{QSqlQuery qsql(*qdata);if (qsql.exec(QString("SELECT * FROM student WHERE stuNumber = %1;").arg(stuNum))){qsql.next();QString result;result += "数学:\t";result += qsql.value(6).toString();result += "\n语文:\t";result += qsql.value(7).toString();result += "\n英语:\t";result += qsql.value(8).toString();result += "\n物理:\t";result += qsql.value(9).toString();result += "\n化学:\t";result += qsql.value(10).toString();ui->scoreTextEdit->setPlainText(result);}else{qDebug() << "fail";}
}void student::on_newsButton_clicked()
{QSqlQuery qsql(*qdata);if (qsql.exec(QString("SELECT * FROM news;"))){QString result;while (qsql.next()){result += qsql.value(0).toString();result += "\t";result += qsql.value(1).toString();result += "\n";result += qsql.value(2).toString();result += "\n\n";}ui->newsTextEdit->appendPlainText(result);}else{qDebug() << "fail";}
}

Widget.cpp

#include "Widget.h"
#include "./ui_Widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);qdata = QSqlDatabase::addDatabase("QSQLITE");qdata.setHostName("localhost");qdata.setDatabaseName("E:\\clangC++\\learnQT\\Learn_24\\database.db");qdata.setUserName("root");qdata.setPassword("root");if (qdata.open()){qDebug() << "OK";}else{qDebug() << "Fail";}
}Widget::~Widget()
{qDebug() << "delete Widget";delete ui;delete stu;delete regist;
}void Widget::on_loginButton_clicked()
{QSqlQuery qsql(qdata);if (qsql.exec(QString("SELECT * FROM student WHERE stuNumber = %1;").arg(ui->stuNumLineEdit->text()))){if (qsql.next()){QString name = qsql.value(0).toString();QString password = qsql.value(1).toString();qDebug() << name << " " << password;if (name == ui->stuNumLineEdit->text() &&password == ui->passwordLineEdit->text()){qDebug() << "show stu";stu = new student(password.toUInt(), &qdata);this->hide();stu->show();}else{qDebug() << "学号或密码错误";}}}else{qDebug() << "Fail";}
}void Widget::on_registButton_clicked()
{QSqlQuery qsql(qdata);bool flag;ui->stuNumLineEdit->text().toUInt(&flag);if (flag &&qsql.exec(QString("SELECT stuNumber FROM student WHERE stuNumber = %1;").arg(ui->stuNumLineEdit->text()))){if (qsql.next()){qDebug() << ui->stuNumLineEdit->text();QMessageBox::information(this, "提示", "学号重复");}else{qDebug() << "show reg";regist = new reg(ui->stuNumLineEdit->text().toUInt(),ui->passwordLineEdit->text(), &qdata);this->hide();regist->show();}}else{qDebug() << "Fail";}
}

ui_Widget.h

/********************************************************************************
** Form generated from reading UI file 'Widget.ui'
**
** Created by: Qt User Interface Compiler version 6.5.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/#ifndef UI_WIDGET_H
#define UI_WIDGET_H#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>QT_BEGIN_NAMESPACEclass Ui_Widget
{
public:QLabel *label;QWidget *layoutWidget;QFormLayout *formLayout;QLabel *label_2;QLineEdit *stuNumLineEdit;QLabel *label_3;QLineEdit *passwordLineEdit;QWidget *layoutWidget1;QHBoxLayout *horizontalLayout;QPushButton *loginButton;QPushButton *registButton;void setupUi(QWidget *Widget){if (Widget->objectName().isEmpty())Widget->setObjectName("Widget");Widget->resize(337, 233);label = new QLabel(Widget);label->setObjectName("label");label->setGeometry(QRect(10, 10, 321, 41));label->setStyleSheet(QString::fromUtf8("font: 700 20pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));label->setAlignment(Qt::AlignCenter);layoutWidget = new QWidget(Widget);layoutWidget->setObjectName("layoutWidget");layoutWidget->setGeometry(QRect(30, 70, 289, 72));formLayout = new QFormLayout(layoutWidget);formLayout->setObjectName("formLayout");formLayout->setContentsMargins(0, 0, 0, 0);label_2 = new QLabel(layoutWidget);label_2->setObjectName("label_2");label_2->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(0, QFormLayout::LabelRole, label_2);stuNumLineEdit = new QLineEdit(layoutWidget);stuNumLineEdit->setObjectName("stuNumLineEdit");stuNumLineEdit->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(0, QFormLayout::FieldRole, stuNumLineEdit);label_3 = new QLabel(layoutWidget);label_3->setObjectName("label_3");label_3->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(1, QFormLayout::LabelRole, label_3);passwordLineEdit = new QLineEdit(layoutWidget);passwordLineEdit->setObjectName("passwordLineEdit");passwordLineEdit->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(1, QFormLayout::FieldRole, passwordLineEdit);layoutWidget1 = new QWidget(Widget);layoutWidget1->setObjectName("layoutWidget1");layoutWidget1->setGeometry(QRect(100, 180, 158, 32));horizontalLayout = new QHBoxLayout(layoutWidget1);horizontalLayout->setObjectName("horizontalLayout");horizontalLayout->setContentsMargins(0, 0, 0, 0);loginButton = new QPushButton(layoutWidget1);loginButton->setObjectName("loginButton");loginButton->setStyleSheet(QString::fromUtf8("font: 700 12pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout->addWidget(loginButton);registButton = new QPushButton(layoutWidget1);registButton->setObjectName("registButton");registButton->setStyleSheet(QString::fromUtf8("font: 700 12pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout->addWidget(registButton);retranslateUi(Widget);QMetaObject::connectSlotsByName(Widget);} // setupUivoid retranslateUi(QWidget *Widget){Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));label->setText(QCoreApplication::translate("Widget", "\345\255\246\347\224\237\344\277\241\346\201\257\347\263\273\347\273\237", nullptr));label_2->setText(QCoreApplication::translate("Widget", "\345\255\246    \345\217\267", nullptr));label_3->setText(QCoreApplication::translate("Widget", "\345\257\206    \347\240\201", nullptr));loginButton->setText(QCoreApplication::translate("Widget", "\347\231\273\345\275\225", nullptr));registButton->setText(QCoreApplication::translate("Widget", "\346\263\250\345\206\214", nullptr));} // retranslateUi};namespace Ui {class Widget: public Ui_Widget {};
} // namespace UiQT_END_NAMESPACE#endif // UI_WIDGET_H

ui_student.h

/********************************************************************************
** Form generated from reading UI file 'student.ui'
**
** Created by: Qt User Interface Compiler version 6.5.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/#ifndef UI_STUDENT_H
#define UI_STUDENT_H#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QFrame>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPlainTextEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QWidget>QT_BEGIN_NAMESPACEclass Ui_student
{
public:QLabel *label;QPlainTextEdit *scoreTextEdit;QPlainTextEdit *newsTextEdit;QFrame *line;QFrame *line_2;QWidget *layoutWidget;QFormLayout *formLayout;QLabel *label_2;QLineEdit *nameLineEdit;QLabel *label_3;QLineEdit *numberLineEdit;QLabel *label_4;QLineEdit *sexLineEdit;QLabel *label_5;QLineEdit *birthdayLineEdit;QLabel *label_6;QLineEdit *phoneLineEdit;QWidget *layoutWidget1;QHBoxLayout *horizontalLayout;QPushButton *stuInfButton;QSpacerItem *horizontalSpacer;QPushButton *phoneChangeButton;QWidget *layoutWidget2;QHBoxLayout *horizontalLayout_2;QLabel *label_7;QSpacerItem *horizontalSpacer_2;QPushButton *scoreButton;QWidget *layoutWidget3;QHBoxLayout *horizontalLayout_3;QLabel *label_8;QSpacerItem *horizontalSpacer_3;QPushButton *newsButton;void setupUi(QWidget *student){if (student->objectName().isEmpty())student->setObjectName("student");student->resize(860, 590);student->setMinimumSize(QSize(860, 590));student->setMaximumSize(QSize(860, 590));label = new QLabel(student);label->setObjectName("label");label->setGeometry(QRect(140, 10, 111, 41));label->setStyleSheet(QString::fromUtf8("font: 700 20pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));scoreTextEdit = new QPlainTextEdit(student);scoreTextEdit->setObjectName("scoreTextEdit");scoreTextEdit->setGeometry(QRect(420, 60, 431, 231));scoreTextEdit->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));newsTextEdit = new QPlainTextEdit(student);newsTextEdit->setObjectName("newsTextEdit");newsTextEdit->setGeometry(QRect(10, 390, 841, 191));newsTextEdit->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));line = new QFrame(student);line->setObjectName("line");line->setGeometry(QRect(7, 313, 851, 21));line->setFrameShape(QFrame::HLine);line->setFrameShadow(QFrame::Sunken);line_2 = new QFrame(student);line_2->setObjectName("line_2");line_2->setGeometry(QRect(396, 5, 20, 318));line_2->setFrameShape(QFrame::VLine);line_2->setFrameShadow(QFrame::Sunken);layoutWidget = new QWidget(student);layoutWidget->setObjectName("layoutWidget");layoutWidget->setGeometry(QRect(10, 60, 381, 186));formLayout = new QFormLayout(layoutWidget);formLayout->setObjectName("formLayout");formLayout->setContentsMargins(0, 0, 0, 0);label_2 = new QLabel(layoutWidget);label_2->setObjectName("label_2");label_2->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(0, QFormLayout::LabelRole, label_2);nameLineEdit = new QLineEdit(layoutWidget);nameLineEdit->setObjectName("nameLineEdit");nameLineEdit->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));nameLineEdit->setReadOnly(true);formLayout->setWidget(0, QFormLayout::FieldRole, nameLineEdit);label_3 = new QLabel(layoutWidget);label_3->setObjectName("label_3");label_3->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(1, QFormLayout::LabelRole, label_3);numberLineEdit = new QLineEdit(layoutWidget);numberLineEdit->setObjectName("numberLineEdit");numberLineEdit->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));numberLineEdit->setReadOnly(true);formLayout->setWidget(1, QFormLayout::FieldRole, numberLineEdit);label_4 = new QLabel(layoutWidget);label_4->setObjectName("label_4");label_4->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(2, QFormLayout::LabelRole, label_4);sexLineEdit = new QLineEdit(layoutWidget);sexLineEdit->setObjectName("sexLineEdit");sexLineEdit->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));sexLineEdit->setReadOnly(true);formLayout->setWidget(2, QFormLayout::FieldRole, sexLineEdit);label_5 = new QLabel(layoutWidget);label_5->setObjectName("label_5");label_5->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(3, QFormLayout::LabelRole, label_5);birthdayLineEdit = new QLineEdit(layoutWidget);birthdayLineEdit->setObjectName("birthdayLineEdit");birthdayLineEdit->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));birthdayLineEdit->setReadOnly(true);formLayout->setWidget(3, QFormLayout::FieldRole, birthdayLineEdit);label_6 = new QLabel(layoutWidget);label_6->setObjectName("label_6");label_6->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(4, QFormLayout::LabelRole, label_6);phoneLineEdit = new QLineEdit(layoutWidget);phoneLineEdit->setObjectName("phoneLineEdit");phoneLineEdit->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(4, QFormLayout::FieldRole, phoneLineEdit);layoutWidget1 = new QWidget(student);layoutWidget1->setObjectName("layoutWidget1");layoutWidget1->setGeometry(QRect(50, 270, 301, 36));horizontalLayout = new QHBoxLayout(layoutWidget1);horizontalLayout->setObjectName("horizontalLayout");horizontalLayout->setContentsMargins(0, 0, 0, 0);stuInfButton = new QPushButton(layoutWidget1);stuInfButton->setObjectName("stuInfButton");stuInfButton->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout->addWidget(stuInfButton);horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);horizontalLayout->addItem(horizontalSpacer);phoneChangeButton = new QPushButton(layoutWidget1);phoneChangeButton->setObjectName("phoneChangeButton");phoneChangeButton->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout->addWidget(phoneChangeButton);layoutWidget2 = new QWidget(student);layoutWidget2->setObjectName("layoutWidget2");layoutWidget2->setGeometry(QRect(420, 10, 421, 39));horizontalLayout_2 = new QHBoxLayout(layoutWidget2);horizontalLayout_2->setObjectName("horizontalLayout_2");horizontalLayout_2->setContentsMargins(0, 0, 0, 0);label_7 = new QLabel(layoutWidget2);label_7->setObjectName("label_7");label_7->setStyleSheet(QString::fromUtf8("font: 700 20pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout_2->addWidget(label_7);horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);horizontalLayout_2->addItem(horizontalSpacer_2);scoreButton = new QPushButton(layoutWidget2);scoreButton->setObjectName("scoreButton");scoreButton->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout_2->addWidget(scoreButton);layoutWidget3 = new QWidget(student);layoutWidget3->setObjectName("layoutWidget3");layoutWidget3->setGeometry(QRect(250, 340, 361, 39));horizontalLayout_3 = new QHBoxLayout(layoutWidget3);horizontalLayout_3->setObjectName("horizontalLayout_3");horizontalLayout_3->setContentsMargins(0, 0, 0, 0);label_8 = new QLabel(layoutWidget3);label_8->setObjectName("label_8");label_8->setStyleSheet(QString::fromUtf8("font: 700 20pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout_3->addWidget(label_8);horizontalSpacer_3 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);horizontalLayout_3->addItem(horizontalSpacer_3);newsButton = new QPushButton(layoutWidget3);newsButton->setObjectName("newsButton");newsButton->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout_3->addWidget(newsButton);retranslateUi(student);QMetaObject::connectSlotsByName(student);} // setupUivoid retranslateUi(QWidget *student){student->setWindowTitle(QCoreApplication::translate("student", "Form", nullptr));label->setText(QCoreApplication::translate("student", "\345\255\246\347\224\237\344\277\241\346\201\257", nullptr));label_2->setText(QCoreApplication::translate("student", "\345\247\223\345\220\215", nullptr));label_3->setText(QCoreApplication::translate("student", "\345\255\246\345\217\267", nullptr));label_4->setText(QCoreApplication::translate("student", "\346\200\247\345\210\253", nullptr));label_5->setText(QCoreApplication::translate("student", "\345\271\264\351\276\204", nullptr));label_6->setText(QCoreApplication::translate("student", "\350\201\224\347\263\273\346\226\271\345\274\217", nullptr));stuInfButton->setText(QCoreApplication::translate("student", "\344\277\241\346\201\257\346\237\245\350\257\242", nullptr));phoneChangeButton->setText(QCoreApplication::translate("student", "\350\201\224\347\263\273\346\226\271\345\274\217\344\277\256\346\224\271", nullptr));label_7->setText(QCoreApplication::translate("student", "\345\255\246\347\224\237\346\210\220\347\273\251", nullptr));scoreButton->setText(QCoreApplication::translate("student", "\346\237\245\350\257\242", nullptr));label_8->setText(QCoreApplication::translate("student", "\345\255\246\346\240\241\345\205\254\345\221\212", nullptr));newsButton->setText(QCoreApplication::translate("student", "\346\237\245\350\257\242", nullptr));} // retranslateUi};namespace Ui {class student: public Ui_student {};
} // namespace UiQT_END_NAMESPACE#endif // UI_STUDENT_H

ui_reg.h

/********************************************************************************
** Form generated from reading UI file 'reg.ui'
**
** Created by: Qt User Interface Compiler version 6.5.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/#ifndef UI_REG_H
#define UI_REG_H#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>QT_BEGIN_NAMESPACEclass Ui_reg
{
public:QLabel *label;QWidget *layoutWidget;QFormLayout *formLayout;QLabel *label_2;QLineEdit *stuNameLineEdit;QLabel *label_3;QLineEdit *stuNumberLineEdit;QLabel *label_4;QLineEdit *sexLineEdit;QLabel *label_5;QLineEdit *birthdayLineEdit;QLabel *label_6;QLineEdit *phoneLineEdit;QWidget *layoutWidget1;QHBoxLayout *horizontalLayout;QPushButton *OkButton;QPushButton *cancelButton;void setupUi(QWidget *reg){if (reg->objectName().isEmpty())reg->setObjectName("reg");reg->resize(400, 335);label = new QLabel(reg);label->setObjectName("label");label->setGeometry(QRect(120, 10, 171, 41));label->setStyleSheet(QString::fromUtf8("font: 700 20pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));layoutWidget = new QWidget(reg);layoutWidget->setObjectName("layoutWidget");layoutWidget->setGeometry(QRect(10, 60, 381, 201));formLayout = new QFormLayout(layoutWidget);formLayout->setObjectName("formLayout");formLayout->setContentsMargins(0, 0, 0, 0);label_2 = new QLabel(layoutWidget);label_2->setObjectName("label_2");label_2->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(0, QFormLayout::LabelRole, label_2);stuNameLineEdit = new QLineEdit(layoutWidget);stuNameLineEdit->setObjectName("stuNameLineEdit");stuNameLineEdit->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(0, QFormLayout::FieldRole, stuNameLineEdit);label_3 = new QLabel(layoutWidget);label_3->setObjectName("label_3");label_3->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(1, QFormLayout::LabelRole, label_3);stuNumberLineEdit = new QLineEdit(layoutWidget);stuNumberLineEdit->setObjectName("stuNumberLineEdit");stuNumberLineEdit->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));stuNumberLineEdit->setReadOnly(true);formLayout->setWidget(1, QFormLayout::FieldRole, stuNumberLineEdit);label_4 = new QLabel(layoutWidget);label_4->setObjectName("label_4");label_4->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(2, QFormLayout::LabelRole, label_4);sexLineEdit = new QLineEdit(layoutWidget);sexLineEdit->setObjectName("sexLineEdit");sexLineEdit->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(2, QFormLayout::FieldRole, sexLineEdit);label_5 = new QLabel(layoutWidget);label_5->setObjectName("label_5");label_5->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(3, QFormLayout::LabelRole, label_5);birthdayLineEdit = new QLineEdit(layoutWidget);birthdayLineEdit->setObjectName("birthdayLineEdit");birthdayLineEdit->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(3, QFormLayout::FieldRole, birthdayLineEdit);label_6 = new QLabel(layoutWidget);label_6->setObjectName("label_6");label_6->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(4, QFormLayout::LabelRole, label_6);phoneLineEdit = new QLineEdit(layoutWidget);phoneLineEdit->setObjectName("phoneLineEdit");phoneLineEdit->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(4, QFormLayout::FieldRole, phoneLineEdit);layoutWidget1 = new QWidget(reg);layoutWidget1->setObjectName("layoutWidget1");layoutWidget1->setGeometry(QRect(130, 280, 158, 39));horizontalLayout = new QHBoxLayout(layoutWidget1);horizontalLayout->setObjectName("horizontalLayout");horizontalLayout->setContentsMargins(0, 0, 0, 0);OkButton = new QPushButton(layoutWidget1);OkButton->setObjectName("OkButton");OkButton->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout->addWidget(OkButton);cancelButton = new QPushButton(layoutWidget1);cancelButton->setObjectName("cancelButton");cancelButton->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout->addWidget(cancelButton);retranslateUi(reg);QMetaObject::connectSlotsByName(reg);} // setupUivoid retranslateUi(QWidget *reg){reg->setWindowTitle(QCoreApplication::translate("reg", "Form", nullptr));label->setText(QCoreApplication::translate("reg", "\345\255\246\347\224\237\344\277\241\346\201\257\345\275\225\345\205\245", nullptr));label_2->setText(QCoreApplication::translate("reg", "\345\247\223\345\220\215", nullptr));stuNameLineEdit->setText(QString());label_3->setText(QCoreApplication::translate("reg", "\345\255\246\345\217\267", nullptr));stuNumberLineEdit->setText(QString());label_4->setText(QCoreApplication::translate("reg", "\346\200\247\345\210\253", nullptr));sexLineEdit->setText(QString());label_5->setText(QCoreApplication::translate("reg", "\345\271\264\351\276\204", nullptr));birthdayLineEdit->setText(QString());label_6->setText(QCoreApplication::translate("reg", "\350\201\224\347\263\273\346\226\271\345\274\217", nullptr));phoneLineEdit->setText(QString());OkButton->setText(QCoreApplication::translate("reg", "\347\241\256\345\256\232", nullptr));cancelButton->setText(QCoreApplication::translate("reg", "\345\217\226\346\266\210", nullptr));} // retranslateUi};namespace Ui {class reg: public Ui_reg {};
} // namespace UiQT_END_NAMESPACE#endif // UI_REG_H

Cmakelists.txt

cmake_minimum_required(VERSION 3.5)project(Learn_24 VERSION 0.1 LANGUAGES CXX)set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets LinguistTools)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets LinguistTools)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Sql)set(TS_FILES Learn_24_zh_CN.ts)set(PROJECT_SOURCESmain.cppWidget.cppWidget.hWidget.uistudent.cppstudent.hstudent.uireg.cppreg.hreg.ui${TS_FILES}
)if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)qt_add_executable(Learn_24MANUAL_FINALIZATION${PROJECT_SOURCES})
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET Learn_24 APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creationqt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
else()if(ANDROID)add_library(Learn_24 SHARED${PROJECT_SOURCES})
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")else()add_executable(Learn_24${PROJECT_SOURCES})endif()qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
endif()target_link_libraries(Learn_24 PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(Learn_24 PRIVATE Qt${QT_VERSION_MAJOR}::Sql)set_target_properties(Learn_24 PROPERTIESMACOSX_BUNDLE_GUI_IDENTIFIER my.example.comMACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}MACOSX_BUNDLE TRUEWIN32_EXECUTABLE TRUE
)install(TARGETS Learn_24BUNDLE DESTINATION .LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})if(QT_VERSION_MAJOR EQUAL 6)qt_finalize_executable(Learn_24)
endif()

管理员端源码:

main.cpp

#include "Widget.h"#include <QApplication>
#include <QLocale>
#include <QTranslator>int main(int argc, char *argv[])
{QApplication a(argc, argv);QTranslator translator;const QStringList uiLanguages = QLocale::system().uiLanguages();for (const QString &locale : uiLanguages) {const QString baseName = "Learn_25_" + QLocale(locale).name();if (translator.load(":/i18n/" + baseName)) {a.installTranslator(&translator);break;}}Widget w;w.show();return a.exec();
}

Widget.cpp

#include "Widget.h"
#include "./ui_Widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);qdata = QSqlDatabase::addDatabase("QSQLITE");qdata.setHostName("localhost");qdata.setDatabaseName("E:\\clangC++\\learnQT\\Learn_24\\database.db");qdata.setUserName("root");qdata.setPassword("root");if (qdata.open()){qDebug() << "OK";}else{qDebug() << "Fail";}
}Widget::~Widget()
{delete ui;delete adm;
}void Widget::on_longinButton_clicked()
{QSqlQuery qsql(qdata);if (qsql.exec(QString("SELECT * FROM login WHERE name = '%1';").arg(ui->nameLineEdit->text()))){while (qsql.next()){if (qsql.value(1).toString() == ui->passwordLineEdit->text()){adm = new admin(&qdata);this->hide();adm->show();break;}}}else{qDebug() << "fail";}
}

Widget.h

#ifndef WIDGET_H
#define WIDGET_H#include "admin.h"
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QWidget>QT_BEGIN_NAMESPACE
namespace Ui
{
class Widget;
}
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_longinButton_clicked();private:Ui::Widget *ui;QSqlDatabase qdata;admin *adm = nullptr;
};
#endif // WIDGET_H

ui_Widget.h

/********************************************************************************
** Form generated from reading UI file 'Widget.ui'
**
** Created by: Qt User Interface Compiler version 6.5.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/#ifndef UI_WIDGET_H
#define UI_WIDGET_H#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>QT_BEGIN_NAMESPACEclass Ui_Widget
{
public:QLabel *label;QPushButton *longinButton;QWidget *layoutWidget;QFormLayout *formLayout;QLabel *label_2;QLineEdit *nameLineEdit;QLabel *label_3;QLineEdit *passwordLineEdit;void setupUi(QWidget *Widget){if (Widget->objectName().isEmpty())Widget->setObjectName("Widget");Widget->resize(304, 217);label = new QLabel(Widget);label->setObjectName("label");label->setGeometry(QRect(80, 10, 141, 37));label->setStyleSheet(QString::fromUtf8("font: 700 20pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));longinButton = new QPushButton(Widget);longinButton->setObjectName("longinButton");longinButton->setGeometry(QRect(110, 170, 91, 31));longinButton->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));layoutWidget = new QWidget(Widget);layoutWidget->setObjectName("layoutWidget");layoutWidget->setGeometry(QRect(10, 70, 291, 72));formLayout = new QFormLayout(layoutWidget);formLayout->setObjectName("formLayout");formLayout->setContentsMargins(0, 0, 0, 0);label_2 = new QLabel(layoutWidget);label_2->setObjectName("label_2");label_2->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(0, QFormLayout::LabelRole, label_2);nameLineEdit = new QLineEdit(layoutWidget);nameLineEdit->setObjectName("nameLineEdit");nameLineEdit->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(0, QFormLayout::FieldRole, nameLineEdit);label_3 = new QLabel(layoutWidget);label_3->setObjectName("label_3");label_3->setStyleSheet(QString::fromUtf8("font: 700 16pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(1, QFormLayout::LabelRole, label_3);passwordLineEdit = new QLineEdit(layoutWidget);passwordLineEdit->setObjectName("passwordLineEdit");passwordLineEdit->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));formLayout->setWidget(1, QFormLayout::FieldRole, passwordLineEdit);retranslateUi(Widget);QMetaObject::connectSlotsByName(Widget);} // setupUivoid retranslateUi(QWidget *Widget){Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));label->setText(QCoreApplication::translate("Widget", "\347\256\241\347\220\206\345\221\230\347\231\273\345\275\225", nullptr));longinButton->setText(QCoreApplication::translate("Widget", "\347\231\273\345\275\225", nullptr));label_2->setText(QCoreApplication::translate("Widget", "\350\264\246\345\217\267", nullptr));nameLineEdit->setText(QCoreApplication::translate("Widget", "root", nullptr));label_3->setText(QCoreApplication::translate("Widget", "\345\257\206\347\240\201", nullptr));passwordLineEdit->setText(QCoreApplication::translate("Widget", "root", nullptr));} // retranslateUi};namespace Ui {class Widget: public Ui_Widget {};
} // namespace UiQT_END_NAMESPACE#endif // UI_WIDGET_H

admin.cpp

#include "admin.h"
#include "ui_admin.h"admin::admin(QSqlDatabase *qdata_, QWidget *parent): QWidget(parent), ui(new Ui::admin), qdata(qdata_)
{ui->setupUi(this);// qtbv = new QSqlTableModel(this, *qdata);qtbv = new myModel(this, *qdata);qtbv->setTable("student");qtbv->select();qtbv->setEditStrategy(QSqlTableModel::OnManualSubmit);qtbv->setHeaderData(0, Qt::Horizontal, "学号");qtbv->setHeaderData(2, Qt::Horizontal, "姓名");qtbv->setHeaderData(6, Qt::Horizontal, "数学");qtbv->setHeaderData(7, Qt::Horizontal, "语文");qtbv->setHeaderData(8, Qt::Horizontal, "英语");qtbv->setHeaderData(9, Qt::Horizontal, "物理");qtbv->setHeaderData(10, Qt::Horizontal, "化学");ui->scoreTableView->setModel(qtbv);ui->scoreTableView->hideColumn(1);ui->scoreTableView->hideColumn(3);ui->scoreTableView->hideColumn(4);ui->scoreTableView->hideColumn(5);
}admin::~admin()
{qDebug() << "delete admin";delete ui;
}void admin::on_newsButton_clicked()
{QSqlQuery qsql(*qdata);if (ui->newsDateLineEdit->text().isEmpty() ||ui->newsTitleLineEdit->text().isEmpty() ||ui->newsTextEdit->toPlainText().isEmpty()){QMessageBox::information(this, "提示", "日期, 标题, 内容不可为空");}else{if (qsql.exec(QString("INSERT INTO news VALUES ('%1', '%2', '%3');").arg(ui->newsDateLineEdit->text()).arg(ui->newsTitleLineEdit->text()).arg(ui->newsTextEdit->toPlainText()))){qDebug() << "OK";}}
}void admin::on_classButton_clicked()
{QSqlQuery qsql(*qdata);if (ui->classDateLineEdit->text().isEmpty() ||ui->classTitleLineEdit->text().isEmpty() ||ui->classTextEdit->toPlainText().isEmpty()){QMessageBox::information(this, "提示", "日期, 标题, 内容不可为空");}else{if (qsql.exec(QString("INSERT INTO news VALUES ('%1', '%2', '%3');").arg(ui->classDateLineEdit->text()).arg(ui->classTitleLineEdit->text()).arg(ui->classTextEdit->toPlainText()))){qDebug() << "OK";}}
}void admin::on_stuScoreButton_clicked()
{if (qtbv->submitAll()){qDebug() << "submitAll OK";}
}

admin.h

#ifndef ADMIN_H
#define ADMIN_H#include "myModel.h"
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QWidget>namespace Ui
{
class admin;
}class admin : public QWidget
{Q_OBJECTpublic:explicit admin(QSqlDatabase *qdata_, QWidget *parent = nullptr);~admin();private slots:void on_newsButton_clicked();void on_classButton_clicked();void on_stuScoreButton_clicked();private:Ui::admin *ui;QSqlDatabase *qdata;myModel *qtbv;// QSqlTableModel *qtbv;
};#endif // ADMIN_H

ui_admin.h

/********************************************************************************
** Form generated from reading UI file 'admin.ui'
**
** Created by: Qt User Interface Compiler version 6.5.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/#ifndef UI_ADMIN_H
#define UI_ADMIN_H#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QFrame>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPlainTextEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QTableView>
#include <QtWidgets/QWidget>QT_BEGIN_NAMESPACEclass Ui_admin
{
public:QFrame *line;QFrame *line_2;QTableView *scoreTableView;QPlainTextEdit *newsTextEdit;QPlainTextEdit *classTextEdit;QWidget *layoutWidget;QHBoxLayout *horizontalLayout_3;QLabel *label_6;QLineEdit *classDateLineEdit;QLabel *label_7;QLineEdit *classTitleLineEdit;QWidget *widget;QHBoxLayout *horizontalLayout;QLabel *label_4;QLineEdit *newsDateLineEdit;QLabel *label_5;QLineEdit *newsTitleLineEdit;QWidget *widget1;QHBoxLayout *horizontalLayout_2;QLabel *label_2;QSpacerItem *horizontalSpacer;QPushButton *newsButton;QWidget *widget2;QHBoxLayout *horizontalLayout_4;QLabel *label_3;QSpacerItem *horizontalSpacer_2;QPushButton *classButton;QWidget *widget3;QHBoxLayout *horizontalLayout_5;QLabel *label;QSpacerItem *horizontalSpacer_3;QPushButton *stuScoreButton;void setupUi(QWidget *admin){if (admin->objectName().isEmpty())admin->setObjectName("admin");admin->resize(1210, 950);admin->setMinimumSize(QSize(1210, 950));admin->setMaximumSize(QSize(1210, 950));line = new QFrame(admin);line->setObjectName("line");line->setGeometry(QRect(7, 530, 1191, 20));line->setFrameShape(QFrame::HLine);line->setFrameShadow(QFrame::Sunken);line_2 = new QFrame(admin);line_2->setObjectName("line_2");line_2->setGeometry(QRect(600, 540, 21, 401));line_2->setFrameShape(QFrame::VLine);line_2->setFrameShadow(QFrame::Sunken);scoreTableView = new QTableView(admin);scoreTableView->setObjectName("scoreTableView");scoreTableView->setGeometry(QRect(10, 60, 1191, 471));scoreTableView->setStyleSheet(QString::fromUtf8("font: 700 12pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));newsTextEdit = new QPlainTextEdit(admin);newsTextEdit->setObjectName("newsTextEdit");newsTextEdit->setGeometry(QRect(10, 677, 591, 261));classTextEdit = new QPlainTextEdit(admin);classTextEdit->setObjectName("classTextEdit");classTextEdit->setGeometry(QRect(618, 677, 583, 261));layoutWidget = new QWidget(admin);layoutWidget->setObjectName("layoutWidget");layoutWidget->setGeometry(QRect(640, 620, 532, 34));horizontalLayout_3 = new QHBoxLayout(layoutWidget);horizontalLayout_3->setObjectName("horizontalLayout_3");horizontalLayout_3->setContentsMargins(0, 0, 0, 0);label_6 = new QLabel(layoutWidget);label_6->setObjectName("label_6");label_6->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout_3->addWidget(label_6);classDateLineEdit = new QLineEdit(layoutWidget);classDateLineEdit->setObjectName("classDateLineEdit");classDateLineEdit->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout_3->addWidget(classDateLineEdit);label_7 = new QLabel(layoutWidget);label_7->setObjectName("label_7");label_7->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout_3->addWidget(label_7);classTitleLineEdit = new QLineEdit(layoutWidget);classTitleLineEdit->setObjectName("classTitleLineEdit");classTitleLineEdit->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout_3->addWidget(classTitleLineEdit);widget = new QWidget(admin);widget->setObjectName("widget");widget->setGeometry(QRect(20, 620, 532, 34));horizontalLayout = new QHBoxLayout(widget);horizontalLayout->setObjectName("horizontalLayout");horizontalLayout->setContentsMargins(0, 0, 0, 0);label_4 = new QLabel(widget);label_4->setObjectName("label_4");label_4->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout->addWidget(label_4);newsDateLineEdit = new QLineEdit(widget);newsDateLineEdit->setObjectName("newsDateLineEdit");newsDateLineEdit->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout->addWidget(newsDateLineEdit);label_5 = new QLabel(widget);label_5->setObjectName("label_5");label_5->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout->addWidget(label_5);newsTitleLineEdit = new QLineEdit(widget);newsTitleLineEdit->setObjectName("newsTitleLineEdit");newsTitleLineEdit->setStyleSheet(QString::fromUtf8("font: 700 14pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout->addWidget(newsTitleLineEdit);widget1 = new QWidget(admin);widget1->setObjectName("widget1");widget1->setGeometry(QRect(230, 550, 281, 47));horizontalLayout_2 = new QHBoxLayout(widget1);horizontalLayout_2->setObjectName("horizontalLayout_2");horizontalLayout_2->setContentsMargins(0, 0, 0, 0);label_2 = new QLabel(widget1);label_2->setObjectName("label_2");label_2->setStyleSheet(QString::fromUtf8("font: 700 20pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout_2->addWidget(label_2);horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);horizontalLayout_2->addItem(horizontalSpacer);newsButton = new QPushButton(widget1);newsButton->setObjectName("newsButton");newsButton->setStyleSheet(QString::fromUtf8("font: 700 18pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout_2->addWidget(newsButton);widget2 = new QWidget(admin);widget2->setObjectName("widget2");widget2->setGeometry(QRect(770, 550, 311, 43));horizontalLayout_4 = new QHBoxLayout(widget2);horizontalLayout_4->setObjectName("horizontalLayout_4");horizontalLayout_4->setContentsMargins(0, 0, 0, 0);label_3 = new QLabel(widget2);label_3->setObjectName("label_3");label_3->setStyleSheet(QString::fromUtf8("font: 700 20pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout_4->addWidget(label_3);horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);horizontalLayout_4->addItem(horizontalSpacer_2);classButton = new QPushButton(widget2);classButton->setObjectName("classButton");classButton->setStyleSheet(QString::fromUtf8("font: 700 18pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout_4->addWidget(classButton);widget3 = new QWidget(admin);widget3->setObjectName("widget3");widget3->setGeometry(QRect(520, 10, 371, 43));horizontalLayout_5 = new QHBoxLayout(widget3);horizontalLayout_5->setObjectName("horizontalLayout_5");horizontalLayout_5->setContentsMargins(0, 0, 0, 0);label = new QLabel(widget3);label->setObjectName("label");label->setStyleSheet(QString::fromUtf8("font: 700 20pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout_5->addWidget(label);horizontalSpacer_3 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);horizontalLayout_5->addItem(horizontalSpacer_3);stuScoreButton = new QPushButton(widget3);stuScoreButton->setObjectName("stuScoreButton");stuScoreButton->setStyleSheet(QString::fromUtf8("font: 700 18pt \"\351\230\277\351\207\214\345\267\264\345\267\264\346\231\256\346\203\240\344\275\223 B\";"));horizontalLayout_5->addWidget(stuScoreButton);retranslateUi(admin);QMetaObject::connectSlotsByName(admin);} // setupUivoid retranslateUi(QWidget *admin){admin->setWindowTitle(QCoreApplication::translate("admin", "Form", nullptr));label_6->setText(QCoreApplication::translate("admin", "\346\227\245\346\234\237", nullptr));label_7->setText(QCoreApplication::translate("admin", "\346\240\207\351\242\230", nullptr));label_4->setText(QCoreApplication::translate("admin", "\346\227\245\346\234\237", nullptr));label_5->setText(QCoreApplication::translate("admin", "\346\240\207\351\242\230", nullptr));label_2->setText(QCoreApplication::translate("admin", "\345\205\254\345\221\212\347\256\241\347\220\206", nullptr));newsButton->setText(QCoreApplication::translate("admin", "\345\217\221\345\270\203", nullptr));label_3->setText(QCoreApplication::translate("admin", "\347\217\255\347\272\247\344\277\241\346\201\257\347\256\241\347\220\206", nullptr));classButton->setText(QCoreApplication::translate("admin", "\345\217\221\345\270\203", nullptr));label->setText(QCoreApplication::translate("admin", "\345\255\246\347\224\237\346\210\220\347\273\251\347\256\241\347\220\206", nullptr));stuScoreButton->setText(QCoreApplication::translate("admin", "\345\217\221\345\270\203", nullptr));} // retranslateUi};namespace Ui {class admin: public Ui_admin {};
} // namespace UiQT_END_NAMESPACE#endif // UI_ADMIN_H

myModel.cpp

#include "myModel.h"myModel::myModel(QObject *parent, const QSqlDatabase &db): QSqlTableModel{parent, db}
{}myModel::~myModel()
{qDebug() << "~myModel";
}Qt::ItemFlags myModel::flags(const QModelIndex &index) const
{if (index.column() == 0 || index.column() == 2){return Qt::ItemIsEnabled | Qt::ItemIsSelectable;}else{return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;}
}

myModel.h

#ifndef MYMODEL_H
#define MYMODEL_H#include <QObject>
#include <QSqlTableModel>class myModel : public QSqlTableModel
{Q_OBJECTpublic:explicit myModel(QObject *parent = nullptr,const QSqlDatabase &db = QSqlDatabase());~myModel();Qt::ItemFlags flags(const QModelIndex &index) const override;signals:
};#endif // MYMODEL_H

总结

二十六 学生信息管理系统
插入介绍: QTableViewQSqlTableModel
QTableView
QSqlTableModel
程序所用数据库表格
程序组成以及界面
学生端源码:
管理员端源码:


点击 <C 语言编程核心突破> 快速C语言入门


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/217425.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android : BottomNavigation底部导航_简单应用

示例图&#xff1a; 1.先创建底部导航需要的图片 res → New → Vector Asset 创建三个矢量图 图片1 baseline_home.xml <vector android:height"24dp" android:tint"#000000"android:viewportHeight"24" android:viewportWidth"24…

nrm 的使用 可以快速切换下载(npm)镜像,解决资源下载慢和运行失败

nrm是什么&#xff1f; 介绍 nrm(npm registry manager) 是 npm 的镜像源管理工具. 有时候国外资源太慢,使用 nrm 可以快速的在 npm 源之间切换 安装 npm install -g nrm 基本使用 查看可选择的源 nrm ls 切换到对应的镜像源 nrm use 对应的镜像 删除镜像源 nrm del 名字 …

深入理解 SVG:开启向量图形的大门(下)

&#x1f90d; 前端开发工程师&#xff08;主业&#xff09;、技术博主&#xff08;副业&#xff09;、已过CET6 &#x1f368; 阿珊和她的猫_CSDN个人主页 &#x1f560; 牛客高级专题作者、在牛客打造高质量专栏《前端面试必备》 &#x1f35a; 蓝桥云课签约作者、已在蓝桥云…

ArcGIS pro与SuperMap根据属性自动填充颜色步骤

GIS项目经常会接触到控规CAD数据&#xff0c;想要把数据转换成GIS图层并发布&#xff0c;需要进行专题配图。研究了一下ArcGIS pro和SuperMap iDesktop的配图&#xff0c;整理一下用到的一些技术思路。 1、Excel表格根据RGB值添加单元格填充颜色 要实现如上效果图&#xff0c;…

【C语言程序设计】循环结构程序设计

目录 前言 一、程序设计第一题 二、程序设计第二题 三、程序设计第三题 总结 &#x1f308;嗨&#xff01;我是Filotimo__&#x1f308;。很高兴与大家相识&#xff0c;希望我的博客能对你有所帮助。 &#x1f4a1;本文由Filotimo__✍️原创&#xff0c;首发于CSDN&#x1f4da…

Armv8/Armv9从入门到精通-课程介绍

通知&#xff0c;Arm二期&#xff0c;咱们也有大合集PDF了&#xff0c;共计1587页&#xff0c;还未完成&#xff0c;后续持续更新和优化中。为了方便大家阅读、探讨、做笔记&#xff0c;特意整了此合集PPT&#xff0c;为了增加标签目录&#xff0c;还特意开了福兮阅读器会员。 …

宇视科技视频监控 main-cgi 文件信息泄露漏洞

宇视科技视频监控 main-cgi 文件信息泄露漏洞 一、产品简介二、漏概述三、复现环境四、漏洞检测手工抓包自动化检测 免责声明&#xff1a;请勿利用文章内的相关技术从事非法测试&#xff0c;由于传播、利用此文所提供的信息或者工具而造成的任何直接或者间接的后果及损失&#…

【C语言程序设计】选择结构程序设计

目录 前言 一、程序阅读 二、程序改错 三、程序设计 总结 &#x1f308;嗨&#xff01;我是Filotimo__&#x1f308;。很高兴与大家相识&#xff0c;希望我的博客能对你有所帮助。 &#x1f4a1;本文由Filotimo__✍️原创&#xff0c;首发于CSDN&#x1f4da;。 &#x1f4e3;如…

[Linux] Tomcat

一、Tomcat相关知识 1.1 Tomcat的简介 Tomcat 是 Java 语言开发的&#xff0c;Tomcat 服务器是一个免费的开放源代码的 Web 应用服务器&#xff0c;是 Apache 软件基金会的 Jakarta 项目中的一个核心项目&#xff0c;由 Apache、Sun 和其他一些公司及个人共同开发而成。Tomc…

19.java绘图

A.Graphics类 Graphics类是java.awt包中的一个类&#xff0c;它用于在图形用户界面&#xff08;GUI&#xff09;或其他图形应用程序中进行绘制。该类通常与Component的paint方法一起使用&#xff0c;以在组件上进行绘制操作。 一些Graphics类的常见用法和方法&#xff1a; 在组…

Flask维护者:李辉

Flask维护者&#xff1a;李辉&#xff0c; 最近看b站的flask相关&#xff0c;发现了这个视频&#xff1a;[PyCon China 2023] 濒危 Flask 扩展拯救计划 - 李辉_哔哩哔哩_bilibili 李辉讲他在维护flask之余&#xff0c;开发了apiflask这个依托flask的框架。GitHub - apiflask/a…

如何通过 SSH 访问 VirtualBox 的虚机

VirtualBox 是一款免费虚机软件。在用户使用它安装了 linux 以后&#xff0c;它默认只提供了控制台的管理画面。 直接使用控制台管理 Linux 没有使用诸如 putty 或者 vscode 这样的 ssh 远程管理工具方便。那么可不可以直接使用 ssh 访问 VirtualBox 上的 Linux 呢&#xff1f…

【Spark精讲】Spark作业执行原理

基本流程 用户编写的Spark应用程序最开始都要初始化SparkContext。 用户编写的应用程序中&#xff0c;每执行一个action操作&#xff0c;就会触发一个job的执行&#xff0c;一个应用程序中可能会生成多个job执行。一个job如果存在宽依赖&#xff0c;会将shuffle前后划分成两个…

同时安装5.7和8.0版本的MySQL

MySQL57安装 下载mysql-5.7.38-winx64.msi&#xff0c;安装管理员身份进入到安装文件夹的bin目录下 如何以管理员身份进入D盘 D:cd D:\Programs\MySQL\MySQL Server 5.7\bin把my.ini复制到mysql5.7安装目录下后 初始化 把配置文件放在安装目录下后&#xff0c;初始化的时候会…

玻色量子袁为出席中国移动第四届科技周量子计算算法与应用分论坛

9月12日&#xff0c;中国移动第四届科技周“量子计算算法与应用”分论坛在北京成功举办&#xff0c;中国移动研究院院长黄宇红发表致辞&#xff0c;中国移动未来研究院院长崔春风全程主持。玻色量子作为光量子计算领域真机测试与场景应用的标杆企业应邀出席&#xff0c;玻色量子…

【Matlab】如何将二阶线性微分方程进行Laplace变换得到传递函数

二阶线性微分方程进行Laplace变换 前言正文代码实现 前言 二阶线性微分方程: 一个二阶线性微分方程通常可以写成如下形式: y ′ ′ ( t ) p ( t ) y ′ ( t ) q ( t ) y ( t ) f ( t ) y^{\prime \prime}(t)p(t) y^{\prime}(t)q(t) y(t)f(t) y′′(t)p(t)y′(t)q(t)y(t)f(…

sleep和wait区别,并且查看线程运行状态

一、sleep和wait区别 区别一&#xff1a;语法使用不同 wait 方法必须配合 synchronized 一起使用&#xff0c;不然在运行时就会抛出 IllegalMonitorStateException 的异常 而 sleep 可以单独使用&#xff0c;无需配合 synchronized 一起使用。 区别二&#xff1a;所属类不同…

掌握iText:轻松处理PDF文档-高级篇-添加水印

前言 iText作为一个功能强大、灵活且广泛应用的PDF处理工具&#xff0c;在实际项目中发挥着重要作用。通过这些文章&#xff0c;读者可以深入了解如何利用iText进行PDF的创建、编辑、加密和提取文本等操作&#xff0c;为日常开发工作提供了宝贵的参考和指导。 掌握iText&…

【Apollo】ubuntu20.04源码安装apollo8.0

官方源码安装教程 https://blog.csdn.net/weixin_45929038/article/details/120113008 安装NVIDIA GPU驱动 Apollo 8.0 的一些模块的编译和运行需要依赖 NVIDIA GPU 环境&#xff08;例如感知模块&#xff09;&#xff0c;如果有编译和运行这类模块的需求&#xff0c;则需要安…

蓝桥杯第一场强者挑战赛(C)SOSdp

之前在cf上面接触过SOSdp&#xff08;子集dp&#xff09;&#xff0c;这里就碰到了。 思路&#xff1a; 异或运算即非进位加法运算&#xff0c;因此如果需要进位的话&#xff0c;那么就无法满足题意&#xff0c;因此条件弱化为不需要进位&#xff0c;也就是不存在同一位上面都是…