1、UI设计,具体效果如下:
登录界面:
学生信息列表汇总显示界面:
学生信息录入界面:
2、项目结构
需要三个ui文件,分别表示登录界面、信息展示界面、信息录入界面。三个界面分别对应三个QDialog子类。
3、代码:
SGS.h和SGS.cpp文件代码:
#pragma once
#include
#include "ui_SGS.h"
#include
#include
#include "SGSlist.h"
class SGS : public QDialog
{
Q_OBJECT
public:
SGS(QWidget *parent = Q_NULLPTR);
void InitConnect();
SGSlist *sl;
private:
Ui::SGSClass ui;
private slots:
void onPushButton();
};
#include "SGS.h"
SGS::SGS(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
InitConnect();
sl = new SGSlist;
}
void SGS::InitConnect()
{
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(onPushButton()));
}
void SGS::onPushButton()
{
if (ui.lineEdit_3->text() == "abc"&&ui.lineEdit_2->text() == "123")
{
qDebug() << "hello";
sl->show();
}
else
{
QMessageBox::information(this, QStringLiteral("提示"),
QStringLiteral("账号或密码错误,请重试!"), QMessageBox::Ok);
}
}
SGSlist.h和SGSlist.cpp文件代码:
#pragma once
#include
#include "ui_SGSlist.h"
#include "SGSinfo.h"
class SGSlist : public QDialog
{
Q_OBJECT
public:
SGSlist(QWidget *parent = Q_NULLPTR);
~SGSlist();
public:
void InitConnect();
SGSinfo *m_sinfo;
private:
Ui::SGSlist ui;
int k;
int Et;
public slots:
void on_pushButton();
void InsertInfo(QStringList);
void on_pushButton_3();
void on_pushButton_2();
void on_pushButton_4();
};
#include "SGSlist.h"
SGSlist::SGSlist(QWidget *parent)
: QDialog(parent),
k(1),
Et(0)
{
ui.setupUi(this);
m_sinfo = new SGSinfo;
InitConnect();
}
SGSlist::~SGSlist()
{
}
void SGSlist::InitConnect()
{
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton()));//插入
connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(on_pushButton_2()));//删除
connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(on_pushButton_3()));//修改
connect(ui.pushButton_4, SIGNAL(clicked()), this, SLOT(on_pushButton_4()));//查询
connect(m_sinfo, SIGNAL(SendInsert(QStringList)), this, SLOT(InsertInfo(QStringList)));
}
void SGSlist::on_pushButton()//添加
{
m_sinfo->show();
m_sinfo->LClear();
}
void SGSlist::InsertInfo(QStringList str)
{
if (Et == 0)
{
for (int i = 0;i < 6;i++)
{
QTableWidgetItem *mm;
mm = new QTableWidgetItem;
mm->setText(str[i]);
ui.tableWidget->setItem(k, i, mm);
}
QTableWidgetItem *mm;
mm = new QTableWidgetItem;
int a = str[3].toInt() + str[4].toInt() + str[5].toInt();
mm->setText(QString::number(a));
ui.tableWidget->setItem(k, 6, mm);
k++;
}
else
{
for (int i = 0;i < 6;i++)
{
QTableWidgetItem *mm;
mm = new QTableWidgetItem;
mm->setText(str[i]);
ui.tableWidget->setItem(Et, i, mm);
}
QTableWidgetItem *mm;
mm = new QTableWidgetItem;
int a = str[3].toInt() + str[4].toInt() + str[5].toInt();
mm->setText(QString::number(a));
ui.tableWidget->setItem(Et, 6, mm);
Et = 0;
}
m_sinfo->close();
}
void SGSlist::on_pushButton_3()//修改
{
if (ui.tableWidget->currentItem())
{
m_sinfo->show();
QStringList str;
int r = ui.tableWidget->currentRow();
for (int i = 0;i < 6;i++)
{
QTableWidgetItem *mm = ui.tableWidget->item(r, i);
str << mm->text();
}
m_sinfo->LEdit(str);
Et = ui.tableWidget->currentRow();
}
}
void SGSlist::on_pushButton_2()//删除
{
if (ui.tableWidget->currentItem())
{
ui.tableWidget->removeRow(ui.tableWidget->currentRow());
k = k - 1;
}
}
void SGSlist::on_pushButton_4()//查询
{
QString ss = ui.lineEdit->text();
if (!ss.isEmpty())
{
QColor cc(Qt::gray);
QList list_1;
list_1 = ui.tableWidget->findItems(ui.lineEdit->text(), Qt::MatchExactly);
if (list_1.size() != 0)
{
int r = list_1[0]->row();
for (int i = 0;i < 7;i++)
{
QTableWidgetItem *mm = ui.tableWidget->item(r, i);
mm->setBackgroundColor(cc);
}
}
}
}
SGSinfo.h和SGSinfo.cpp文件代码:
#pragma once
#include
#include "ui_SGSinfo.h"
#include
class SGSinfo : public QDialog
{
Q_OBJECT
public:
SGSinfo(QWidget *parent = Q_NULLPTR);
~SGSinfo();
void LClear();
void LEdit(QStringList);
signals:
void SendInsert(QStringList);
private:
Ui::SGSinfo ui;
private slots:
void on_pushButton();
};
#include "SGSinfo.h"
SGSinfo::SGSinfo(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton()));
}
SGSinfo::~SGSinfo()
{
}
void SGSinfo::on_pushButton()
{
qDebug() << "vitory";
QStringList strList;
strList << ui.lineEdit_3->text();//学号
strList << ui.lineEdit_6->text();//姓名
strList << ui.lineEdit_7->text();//性别
strList << ui.lineEdit_2->text();//语文
strList << ui.lineEdit->text();//数学
strList << ui.lineEdit_4->text();//英语
emit SendInsert(strList);
}
void SGSinfo::LClear()
{
ui.lineEdit->clear();
ui.lineEdit_2->clear();
ui.lineEdit_3->clear();
ui.lineEdit_4->clear();
ui.lineEdit_6->clear();
ui.lineEdit_7->clear();
}
void SGSinfo::LEdit(QStringList str)
{
ui.lineEdit_3->setText(str[0]);
ui.lineEdit_6->setText(str[1]);
ui.lineEdit_7->setText(str[2]);
ui.lineEdit_2->setText(str[3]);
ui.lineEdit->setText(str[4]);
ui.lineEdit_4->setText(str[5]);
}
main.cpp文件代码:
#include "SGS.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SGS w;
w.show();
return a.exec();
}
4、运行界面
录入信息:
信息录入完毕:
单击表格选中某一单元格,点击修改按钮,进入修改界面:
查询(搜索到记录会成灰色显示):