2023-12-05 Qt学习总结10
点击 <C 语言编程核心突破> 快速C语言入门
Qt学习总结
前言
要解决问题: 学习qt最核心知识, 多一个都不学.
二十六 学生信息管理系统
用Qt实现一个含界面的简单学生信息管理系统, 主要功能:
-
登录和注册功能:系统需要有注册和登录功能,学生可以通过注册账号并登录来使用系统。
-
学生信息录入:系统需要支持学生信息的录入和管理,包括学生姓名、学号、性别、年龄、联系方式等信息。
-
学生信息查询:系统需要支持信息查询功能,学生可以通过查询功能查看自己的个人信息。
-
学生信息修改:学生可以随时修改自己的个人信息,如联系方式。
-
学生成绩管理:系统需要支持学生成绩信息的录入和管理,包括学生课程成绩等信息。
-
学生成绩查询:学生可以通过系统查询自己的课程成绩。
-
班级信息管理:系统需要支持班级信息的录入和管理,包括班级名称、班级简介等信息。
-
公告管理:系统需要支持公告管理功能,管理员可以通过系统发布学校或班级的公告通知。
-
系统安全:系统需要有安全保障措施,防止信息泄露或被恶意攻击。
-
界面友好:系统需要具备友好的用户界面,方便学生和管理员使用。
插入介绍: QTableView
和QSqlTableModel
QTableView
QTableView
是Qt框架中的一个控件,用于显示和编辑过滤表格数据。
它是一个基于模型/视图架构的控件,通过使用模型来管理数据,视图来显示数据。
以下是使用QTableView
的一些常见方法和C++函数:
- 设置模型
可以通过setModel()
函数将一个QAbstractTableModel
类型的模型设置给QTableView
控件。
模型通过继承QAbstractTableModel
类实现,它负责管理数据。
常用的模型类有QStandardItemModel
和QSqlTableModel
。
- 设置选择模式
通过setSelectionBehavior()
和setSelectionMode()
函数设置选择模式,用于指定用户如何在表格中选择行或单元格。
其中,setSelectionBehavior()
函数指定用户选择行或单元格时的行为,而setSelectionMode()
函数指定用户可以选择的行数。
- 设置表头
通过setHorizontalHeader()
和setVerticalHeader()
函数设置自定义表头。
- 设置单元格属性
可以通过setItemDelegate()
函数设置一个委托类,以控制QTableView
中每个单元格的显示和编辑行为.
- 隐藏列
通过hideColumn()
函数隐藏某一列.
QSqlTableModel
QSqlTableModel
是Qt提供的一种数据库模型类,用于提供对单表数据的读写、修改、删除等操作。它继承于QAbstractTableModel
,可以在TableView
中使用。
使用方法:
-
首先需要创建数据库连接
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("myDB.sqlite"); if(!db.open()) { qDebug()<<"open database error!"; return ; }
-
创建
QSqlTableModel
对象QSqlTableModel *model = new QSqlTableModel(this, db); // this表示当前类是model的父类,对象被删除时会自动删除model对象 model->setTable("student"); model->setSort(0, Qt::AscendingOrder); // 按照某一列升序排序 model->select(); // 获取所有数据
-
在
TableView
中显示数据ui->tableView->setModel(model);
-
更新表格中第一行的数据:
QModelIndex index = model->index(0, 1); // 获取第一行第二列的index model->setData(index, "NewValue", Qt::EditRole); // 设置新值 model->submitAll(); // 提交更改
常用函数
-
setTable(const QString &tableName)
:设置表名 -
setFilter(const QString &filter)
:设置筛选条件 -
setSort(int column, Qt::SortOrder order)
:设置排序 -
select()
:从表中获取数据 -
setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole)
:更新数据 -
insertRows(int row, int count, const QModelIndex &parent = QModelIndex())
:插入行数据 -
removeRows(int row, int count, const QModelIndex &parent = QModelIndex())
:删除行数据 -
setEditStrategy(EditStrategy strategy)
:置修改策略,决定了如何在模型中更改数据,并决定在什么条件下提交更改到数据库。SqlTableModel
提供了三种修改策略:QSqlTableModel::OnFieldChange
(默认值)当编辑器中的一个单元格的内容改变时,自动提交该单元格的内容。QSqlTableModel::OnRowChange
当编辑器中的一整行的内容改变时,自动提交该行的内容。QSqlTableModel::OnManualSubmit
不自动提交更改,需要调用submitAll()
函数手动提交。
程序所用数据库表格
需要三个表:
login
这个给管理员使用, 校对管理员密码, 我们这里设置默认用户名和密码都是root, 可以直接在系统下改
CREATE TABLE login(
name varchar(16) NOT NULL,
stuPassword varchar(16) NOT NULL
);
news
这个是储存班级和学校通知
CREATE TABLE news(
theDate date NOT NULL,
theTitle varchar(256) NOT NULL,
theArtical varchar(2048) NOT NULL
);
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_OBJECT
public:
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_OBJECT
public:
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_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
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_NAMESPACE
class 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);
} // setupUi
void 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 Ui
QT_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_NAMESPACE
class 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);
} // setupUi
void 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 Ui
QT_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_NAMESPACE
class 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);
} // setupUi
void 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 Ui
QT_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_SOURCES
main.cpp
Widget.cpp
Widget.h
Widget.ui
student.cpp
student.h
student.ui
reg.cpp
reg.h
reg.ui
${TS_FILES}
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(Learn_24
MANUAL_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-creation
qt_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 PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
install(TARGETS Learn_24
BUNDLE 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_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
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_NAMESPACE
class 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);
} // setupUi
void 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 Ui
QT_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_OBJECT
public:
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_NAMESPACE
class 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);
} // setupUi
void 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 Ui
QT_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_OBJECT
public:
explicit myModel(QObject *parent = nullptr,
const QSqlDatabase &db = QSqlDatabase());
~myModel();
Qt::ItemFlags flags(const QModelIndex &index) const override;
signals:
};
#endif // MYMODEL_H
总结
二十六 学生信息管理系统
插入介绍: QTableView
和QSqlTableModel
QTableView
QSqlTableModel
程序所用数据库表格
程序组成以及界面
学生端源码:
管理员端源码:
点击 <C 语言编程核心突破> 快速C语言入门
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!