Qt实现登录界面(Mysql版本)
思路
我们设计一个登录界面,那么他就应该具有登录和注册两个功能。那么我们这次就来写一下Qt的登录和注册,首先我们设计两个界面,如下图:
login为登陆界面, Register为注册界面。
代码
pro文件
QT += core gui \
sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked 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 it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
form.cpp \
main.cpp \
widget.cpp
HEADERS += \
config.h \
form.h \
widget.h
FORMS += \
form.ui \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
config.h
++
这个地方还需要大家自己填写一下,数据库的参数
++
#ifndef CONFIG_H
#define CONFIG_H
#define SQL_BASE "QMYSQL" //用哪种数据库
#define SQL_HOSTNAME "x.x.x.x" //数据库服务器IP
#define SQL_USERNAME "xxx" //用户名
#define SQL_PASSWORD "xxx" //密码
#define SQL_DATA_1 "xxx" //所要连接的表
#endif // CONFIG_H
form.h
#ifndef FORM_H
#define FORM_H
#include <QWidget>
#include <widget.h>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = nullptr);
~Form();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::Form *ui;
bool flag2 = false;
QString register_ID; // ID Qstring
QString register_pwd; // password Qstring
bool flag_len_Password = false;
};
#endif // FORM_H
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <form.h>
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_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::Widget *ui;
QString ID_get;
QString Password_get;
bool flag1 = false;
bool login_flag = false;
};
#endif // WIDGET_H
form.cpp
#include "form.h"
#include "ui_form.h"
#include <QDebug>
#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlQuery>
#include <config.h>
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
QSqlDatabase db = QSqlDatabase::addDatabase(SQL_BASE);
db.setHostName(SQL_HOSTNAME);
db.setPort(3306);
db.setDatabaseName(SQL_DATA_1);
db.setUserName(SQL_USERNAME);
db.setPassword(SQL_PASSWORD);
bool ok = db.open();
if (ok){
qDebug()<<"sql connect success!";
flag2 = true;
}
else {
qDebug()<<"error open database!";
}
}
Form::~Form()
{
delete ui;
}
void Form::on_pushButton_clicked()
{
register_ID = ui->lineEdit->text();
register_pwd = ui->lineEdit_2->text();
QSqlQuery query;
query.prepare("insert into test_qt_sql(ID, Password) values(?, ?)");//?占位符
//给字段设置内容 list
QVariantList IDlist;
IDlist<<register_ID;
QVariantList pwdlist;
pwdlist<<register_pwd;
//给字段绑定相应的值,顺序绑定
query.addBindValue(IDlist);
query.addBindValue(pwdlist);
//执行预处理命令
query.execBatch();
on_pushButton_2_clicked();
QMessageBox::information(this, "success", "success!");
}
void Form::on_pushButton_2_clicked()
{
Widget *widget = new Widget;
widget->show();
this->close();
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlQuery>
#include <config.h>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
QSqlDatabase db = QSqlDatabase::addDatabase(SQL_BASE);
db.setHostName(SQL_HOSTNAME);
db.setPort(3306);
db.setDatabaseName(SQL_DATA_1);
db.setUserName(SQL_USERNAME);
db.setPassword(SQL_PASSWORD);
bool ok = db.open();
if (ok){
qDebug()<<"sql connect success!";
flag1 = true;
}
else {
qDebug()<<"error open database!";
}
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
ID_get = ui->lineEdit->text(); // 获取lineedit中输入的文本,并存储到Qstring类型变量ID_get中
Password_get = ui->lineEdit_2->text(); // 获取lineedit_2中输入的文本,并存储到Qstring类型变量Password_get中
qDebug() << ID_get;
qDebug() << Password_get;
if (flag1){ // flag1是一个标记位,用于记录数据库是否连接成功,如果未来凝结成功的话,就不在对连接及逆行判断。
QSqlQuery query1("select * from test_qt_sql"); // 查询表的内容
while (query1.next()) {
QString country = query1.value(0).toString(); // test_qt_sql表第1个字段的内容
if (country == ID_get){
qDebug()<<"OK";
QString country = query1.value(1).toString();//test_qt_sql表第2个字段的内容
if (country == Password_get){
QMessageBox::information(this, "success", "login success!");
login_flag = true;
}
}
}
if (login_flag == false){
QMessageBox::information(this, "error", "ID or Password error!");
}
}
else{
QMessageBox::information(this, "error", "Mysql error!");
}
}
void Widget::on_pushButton_2_clicked()
{
Form *form = new Form;
this->close();
form->show();
}
main.cpp
#include "widget.h"
#include <QApplication>
#include <QPropertyAnimation>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
QPropertyAnimation *animation = new QPropertyAnimation(&w,"windowOpacity");
animation->setDuration(1000);
animation->setStartValue(0);
animation->setEndValue(1);
animation->start();
w.show();
return a.exec();
}
**
工程项目,大家可以在下方评论区留下邮箱,我会及时发送的。
或者添加博主微信scc18920209716scczZz
**
版权声明:本文为weixin_50153843原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。