一、项目演示
1、登录界面
2、注册界面
3、登录成功
4、主界面
二、项目预览
三、项目源码
1、ShipDataManagementSystem.pro
#-------------------------------------------------
#
# Project created by QtCreator 2022-08-16T10:49:17
#
#-------------------------------------------------
QT += core gui sql concurrent network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = ShipDataManagementSystem
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
CONFIG += resources_big
SOURCES += \
main.cpp \
ShipDataManagementSystem.cpp \
Login.cpp \
SignUp.cpp
HEADERS += \
ShipDataManagementSystem.h \
Login.h \
SignUp.h
FORMS += \
ShipDataManagementSystem.ui \
Login.ui \
SignUp.ui
INCLUDEPATH += $$PWD/include
LIBS += -L$$PWD/lib -lDataBaseShow -lJsonData -lRecData
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
res.qrc
2、Login.h
#ifndef LOGIN_H
#define LOGIN_H
#include <QDialog>
#include <QSqlDatabase> //数据驱动
#include <QSqlQuery> //数据库执行语句
#include <QCryptographicHash> //加密
#include <QMessageBox>//消息盒子
#include <QSqlError>
#include <QDebug>
#include <QDir>
#include <QGraphicsDropShadowEffect>
#include <QLabel>
#include <QMouseEvent>
extern QString passwd;
extern QString Currentname;
void mysql_Init(); //界面初始化
namespace Ui {
class Login;
}
class Login : public QDialog
{
Q_OBJECT
public:
explicit Login(QWidget *parent = nullptr);
~Login();
private slots:
void on_btn_login_clicked();
void on_btn_register_clicked();
void on_Exit_clicked();
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
Ui::Login *ui;
QPoint last;
};
#endif // LOGIN_H
3、Login.cpp
#pragma execution_character_set("utf-8")
#include "Login.h"
#include "ui_Login.h"
#include "SignUp.h"
#include "ShipDataManagementSystem.h"
QString passwd;
QString Currentname;
Login::Login(QWidget *parent) :
QDialog(parent),
ui(new Ui::Login)
{
ui->setupUi(this);
//控制窗口大小
//this->resize(1920,1080);
//设置图标
this->setWindowIcon(QIcon(":/images/icon.png"));
//设置窗体透明
this->setAttribute(Qt::WA_TranslucentBackground, true);
//设置无边框
this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
//实例阴影shadow
QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
//设置阴影距离
shadow->setOffset(-3, 0);
//设置阴影颜色
shadow->setColor(QColor("#888888"));
//设置阴影圆角
shadow->setBlurRadius(30);
//给嵌套QWidget设置阴影
ui->label_background->setGraphicsEffect(shadow);
//给垂直布局器设置边距(此步很重要, 设置宽度为阴影的宽度)
ui->label_background->setContentsMargins(1,1,1,1);
}
Login::~Login()
{
delete ui;
}
void mysql_Init()
{
//初始化数据库
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setPort(3306);
db.setDatabaseName("userdb");
db.setUserName("root");
db.setPassword("root");
if(!db.open())
{
qDebug()<<"Sql connect failed.";
qDebug() << db.lastError().text();
//QMessageBox::warning(nullptr, "警告", "无法连接数据库");
}
QString createUserTable;
createUserTable ="CREATE TABLE IF NOT EXISTS userdb \
( \
id bigint not null auto_increment , \
username varchar(255) unique NOT NULL, \
password varchar(255) NOT NULL , \
time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,\
PRIMARY KEY (id) \
);";
QSqlQuery query;
query.prepare(createUserTable);
if(!query.exec()){
qDebug()<<"CREATE TABLE Rule:"<<query.lastError();
}
else{
qDebug()<<"CREATE TABLE Rule success!";
}
}
//登录
void Login::on_btn_login_clicked()
{
mysql_Init();
QString username = ui->lineEdit_username->text();
QString password = ui->lineEdit_password->text();
QString sql=QString("select * from userdb where username='%1' and password='%2'")
.arg(username).arg(password);
//创建执行语句对象
QSqlQuery query(sql);
//判断执行结果
if(!query.next())
{
qDebug()<<"Login error";
QMessageBox::information(this,QString::fromUtf8("登录认证"),QString::fromUtf8("登录失败,账户或者密码错误"));
}
else
{
Currentname = ui->lineEdit_username->text();
qDebug()<<"Login success";
QMessageBox::information(this,"登录认证","登录成功");
ShipDataManagementSystem *sdms = new ShipDataManagementSystem;
sdms->show();
this->close();
}
}
//注册
void Login::on_btn_register_clicked()
{
SignUp *reg = new SignUp;
reg->show();
this->hide();
}
//关闭
void Login::on_Exit_clicked()
{
this->close();
}
//鼠标按下
void Login::mousePressEvent(QMouseEvent *event)
{
last = event->globalPos();
}
//鼠标移动
void Login::mouseMoveEvent(QMouseEvent *event)
{
int dx = event->globalX() - last.x();
int dy = event->globalY() - last.y();
last = event->globalPos();
move(x()+dx, y()+dy);
}
//鼠标释放
void Login::mouseReleaseEvent(QMouseEvent *event)
{
int dx = event->globalX() - last.x();
int dy = event->globalY() - last.y();
move(x()+dx, y()+dy);
}
4、SignUp.h
#ifndef SIGNUP_H
#define SIGNUP_H
#include <QWidget>
#include <QMessageBox>
#include <QSqlDatabase>
namespace Ui {
class SignUp;
}
class SignUp : public QWidget
{
Q_OBJECT
public:
explicit SignUp(QWidget *parent = nullptr);
~SignUp();
bool checkSql(QString username); //连接数据库验证账号密码
private slots:
void on_btn_sureadd_clicked();
void on_btn_return_clicked();
private:
Ui::SignUp *ui;
QString username;
QString password;
QString surepass;
QSqlDatabase db;
};
#endif // SIGNUP_H
5、SignUp.cpp
#pragma execution_character_set("utf-8")
#include "SignUp.h"
#include "ui_SignUp.h"
#include "Login.h"
SignUp::SignUp(QWidget *parent) :
QWidget(parent),
ui(new Ui::SignUp)
{
ui->setupUi(this);
this->setWindowTitle(QString::fromUtf8("账号注册"));
this->setWindowIcon(QIcon(":/images/icon.png"));
}
SignUp::~SignUp()
{
delete ui;
}
void SignUp::on_btn_sureadd_clicked()
{
mysql_Init();
username = ui->lineEdit_newname->text();
password = ui->lineEdit_newpass->text();
surepass = ui->lineEdit_surepass->text();
//限制密码需要大于6位
int n = 0;
for (int i = 0;i < password.trimmed().length();i++) //trimmed()去掉前后空格
{
n++;
}
if (password.trimmed().length() < 6)
{
QMessageBox::information(this, "注册认证", "密码设置需要超过6位,请重新设置");
return;
}
//判断密码是否一致
if(password == surepass)
{
QString sql=QString("insert into userdb(username,password) values('%1','%2');")
.arg(username).arg(password);
//创建执行语句对象
QSqlQuery query;
//判断执行结果
if(!query.exec(sql))
{
qDebug()<<"insert into error";
QMessageBox::information(this,"注册认证","注册失败!");
}
else
{
qDebug()<<"insert into success";
QMessageBox::information(this,"注册认证","注册成功!");
Login *lgin = new Login;
lgin->show();
this->close();
}
}else{
QMessageBox::information(this,"注册认证","两次密码输入不一致");
}
}
//返回登录
void SignUp::on_btn_return_clicked()
{
Login *lgin = new Login;
lgin->show();
this->close();
}
6、ShipDataManagementSystem.h
#ifndef SHIPDATAMANAGEMENTSYSTEM_H
#define SHIPDATAMANAGEMENTSYSTEM_H
#include "DataBaseShow.h"
#include "jsondata.h"
#include "RecData.h"
#include <QMainWindow>
#include <QLabel>
namespace Ui {
class ShipDataManagementSystem;
}
class ShipDataManagementSystem : public QMainWindow
{
Q_OBJECT
public:
explicit ShipDataManagementSystem(QWidget *parent = nullptr);
~ShipDataManagementSystem();
private:
Ui::ShipDataManagementSystem *ui;
QLabel *pTimeLabel;
DataBaseShow* dbs;
JsonData* jd;
RecData* rd;
QPoint mousePoint; //鼠标相对于窗体的位置
bool isMousePressed; //鼠标是否按下
public:
void timeUpdate();
private slots:
void on_CollectDataBtn_clicked();
void on_SubjectJsonBtn_clicked();
void on_DataBaseShowBtn_clicked();
void on_ExitSystemBtn_clicked();
protected:
virtual void mousePressEvent(QMouseEvent *event); //鼠标按下事件
virtual void mouseMoveEvent(QMouseEvent *event); //窗体拖动事件
virtual void mouseReleaseEvent(QMouseEvent *event);//鼠标释放事件
};
#endif // SHIPDATAMANAGEMENTSYSTEM_H
7、ShipDataManagementSystem.cpp
#pragma execution_character_set("utf-8")
#include "ShipDataManagementSystem.h"
#include "ui_ShipDataManagementSystem.h"
#include <QToolBar>
#include <QLabel>
#include <QProgressBar>
#include <QTimer>
#include <QDateTime>
#include <QFont>
#include <QMouseEvent>
ShipDataManagementSystem::ShipDataManagementSystem(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ShipDataManagementSystem)
{
ui->setupUi(this);
this->setWindowTitle(QString::fromUtf8("无人船综合管理平台"));
this->setWindowIcon(QIcon(":/images/icon.png"));
//this->resize(1600,1056);
//this->setStyleSheet("background-color: rgb(16, 26, 51);");
this->setStyleSheet("background-image");
QPalette palette;
QPixmap pixmap = QPixmap(":/images/ship3.png").scaled(this->size());
palette.setBrush(QPalette::Window,QBrush(pixmap));
this->setPalette(palette);
setStyleSheet(
/**正常情况下样式**/
"QPushButton{"
"font: 10pt '微软雅黑';"
"color: #2f3640;"
"background-color: #f5f6fa;"
"border-color: #2f3640;"
"border-radius: 15px;"
"border-style: solid;"
"border-width: 2px;"
"padding: 5px;"
"}"
/**鼠标停留在按钮上的样式**/
"QPushButton::hover{"
"color: #FFFFFF;"
"background-color: #718093;"
"border-color: #2f3640;"
"}"
/**鼠标按压下去的样式**/
"QPushButton::pressed,QPushButton::checked{"
"color: #FFFFFF;"
"background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 #273c75, stop:1 #487eb0);"
"}"
/**按钮失能情况下样式**/
"QPushButton::disabled{"
"color: #FFFFFF;"
"background-color: #dcdde1;"
"border-color: #dcdde1;"
"}");
//隐藏标题栏
//setWindowFlags(Qt::FramelessWindowHint);//无边框
//setWindowFlags(Qt::CustomizeWindowHint);//鼠标可缩放窗口
QStatusBar *pStatusBar = statusBar();
pStatusBar->setStyleSheet("background-color:rgb(192,192,192);");
QLabel *pWelCome = new QLabel(QString::fromUtf8("当前状态:"));
pStatusBar->addWidget(pWelCome);
pTimeLabel = new QLabel();
pStatusBar->addPermanentWidget(pTimeLabel);
QTimer *pTimer = new QTimer(this);
pTimer->start();
connect(pTimer,&QTimer::timeout,this,&ShipDataManagementSystem::timeUpdate);
QLabel *locationLabel = new QLabel("主窗口显示"); // 新建状态栏显示部件(QLabel)
//locationLabel->setAlignment(Qt::AlignHcenter); // 设置显示部件的显示方试
locationLabel->setMinimumSize(locationLabel->sizeHint()); //设置显示部件合适的最小尺寸
statusBar()->addWidget(locationLabel); //添加显示部件到状态栏
}
ShipDataManagementSystem::~ShipDataManagementSystem()
{
delete ui;
}
void ShipDataManagementSystem::timeUpdate()
{
//更新时间的槽函数中的代码
QDateTime currentTime = QDateTime::currentDateTime();
QString timeStr = currentTime.toString(QStringLiteral("yyyy/MM/dd hh:mm:ss"));
pTimeLabel->setText(timeStr);
}
void ShipDataManagementSystem::on_CollectDataBtn_clicked()
{
rd = new RecData;
rd->setWindowTitle("Receive Data");
rd->show();
}
void ShipDataManagementSystem::on_SubjectJsonBtn_clicked()
{
jd = new JsonData;
jd->setWindowTitle("Json数据解析界面");
jd->show();
}
void ShipDataManagementSystem::on_DataBaseShowBtn_clicked()
{
dbs = new DataBaseShow;
dbs->setWindowTitle("数据库显示界面");
dbs->show();
}
void ShipDataManagementSystem::on_ExitSystemBtn_clicked()
{
this->close();
}
//鼠标按下事件
void ShipDataManagementSystem::mousePressEvent(QMouseEvent *event)
{
mousePoint = event->pos(); //鼠标相对于窗体的位置
isMousePressed = true; //鼠标按下
event->accept();
}
//窗体拖动事件
void ShipDataManagementSystem::mouseMoveEvent(QMouseEvent *event)
{
//若鼠标左键被按下
if(isMousePressed == true)
{
//鼠标相对于屏幕的位置
QPoint curMousePoint = event->globalPos() - mousePoint;
//移动主窗体位置
move(curMousePoint);
}
event->accept();
}
//鼠标释放事件
void ShipDataManagementSystem::mouseReleaseEvent(QMouseEvent *event)
{
//鼠标未按下
isMousePressed = false;
event->accept();
}
8、main.cpp
#include "ShipDataManagementSystem.h"
#include "Login.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ShipDataManagementSystem w;
//w.show();
Login* lgin = new Login;
lgin->show();
return a.exec();
}