一、描述
有登录界面、注册界面,用到sqlite数据库保存账号和密码,界面还没有布局美化等,只实现了最基本的功能。
二、界面及功能介绍
1、登录界面2、注册界面
3、主界面
没想好弄啥功能,于是就弄了两个按钮。
4、文件结构
三、代码
1.login.pro
QT += core gui
QT += sql widgets
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 += \
general.cpp \
logindialog.cpp \
logondialog.cpp \
main.cpp \
widget.cpp
HEADERS += \
general.h \
logindialog.h \
logondialog.h \
widget.h
FORMS += \
logindialog.ui \
logondialog.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
2.general.h
#ifndef GENERAL_H
#define GENERAL_H
#include <QSqlDatabase>
#include <QDebug>
#include <QStringList>
#include <QSqlQuery>
#include <iostream>
#include <QMessageBox>
QSqlQuery connect_sqlite();
void sql_close();
#endif // GENERAL_H
3.logindialog.h
#ifndef LOGINDIALOG_H
#define LOGINDIALOG_H
#include <QDialog>
#include "logondialog.h"
#include "ui_logondialog.h"
namespace Ui {
class LoginDialog;
}
class LoginDialog : public QDialog
{
Q_OBJECT
public:
explicit LoginDialog(QWidget *parent = nullptr);
~LoginDialog();
LogonDialog logondialog;
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
private:
Ui::LoginDialog *ui;
};
#endif // LOGINDIALOG_H
4.logondialog.h
#ifndef LOGONDIALOG_H
#define LOGONDIALOG_H
#include <QDialog>
#include "general.h"
namespace Ui {
class LogonDialog;
}
class LogonDialog : public QDialog
{
Q_OBJECT
public:
explicit LogonDialog(QWidget *parent = nullptr);
~LogonDialog();
public:
Ui::LogonDialog *ui;
private slots:
void on_pushButton_2_clicked();
void on_pushButton_clicked();
};
#endif // LOGONDIALOG_H
5.widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "logindialog.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_2_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
6.general.cpp
#include "general.h"
QSqlDatabase db;
QSqlQuery connect_sqlite()
{
//验证数据库连接是否存在,不存在就创建一个
if(QSqlDatabase::contains("first"))
{
db = QSqlDatabase::database("first");
}
else
{
//用的是sqlite数据库
db = QSqlDatabase::addDatabase("QSQLITE","first");
db.setDatabaseName("QtQq.db");//设置数据库名称,若不存在自动创建一个
db.setUserName("zhanglinghua");//设置数据库账号,不用也没啥问题
db.setPassword("123456"); //设置数据库密码,不用也没啥问题
}
bool a = db.open(); //打开数据库,必须打开才能进行增删查改的操作
if(!a)
{
qDebug()<<"数据库打开失败";
exit(100);
}
else
{
qDebug()<<"数据库打开成功";
}
QSqlQuery sql_query(db);
return sql_query;
}
void sql_close()
{
db.close();
}
7.logindialog.cpp
#include "logindialog.h"
#include "ui_logindialog.h"
LoginDialog::LoginDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::LoginDialog)
{
ui->setupUi(this);
}
LoginDialog::~LoginDialog()
{
delete ui;
}
//验证登录函数
void LoginDialog::on_pushButton_clicked()
{
QSqlQuery sql_query = connect_sqlite();
QString user = ui->lineEdit->text();//获取账号
QString pwd = ui->lineEdit_2->text();//获取密码
//qDebug()<<user<<pwd;
//先验证账号是否存在,若存在,再验证密码
QString user_sql = QString("select user from users;");
sql_query.exec(user_sql);
bool flag=0;
while(sql_query.next())
{
if(sql_query.value(0).toString()==user)
{
flag=1;
}
}
if(flag==1)
{
QString sql = QString("select passwd from users where user = '%1'").arg(user);//验证账号密码
sql_query.prepare(sql);
sql_query.exec();
QString sql_pwd;
while(sql_query.next())
{
sql_pwd = sql_query.value(0).toString();
qDebug()<<"密码"<<sql_pwd;
}
if(pwd == sql_pwd)
{
qDebug()<<"登录成功";
accept();
}
else
{
QMessageBox::information(this,QObject::tr("登录提示"),QObject::tr("账号或密码错误"),QMessageBox::Ok,QMessageBox::Ok);
}
}
else
{
QMessageBox::information(this,QObject::tr("登录提示"),QObject::tr("未发现该账号,请先注册。"),QMessageBox::Ok,QMessageBox::Ok);
}
}
//关闭注册窗口
void LoginDialog::on_pushButton_2_clicked()
{
close();
}
//显示注册窗口
void LoginDialog::on_pushButton_3_clicked()
{
LogonDialog lgo;
lgo.setWindowTitle("注册");
lgo.exec();
}
//忘记密码函数
void LoginDialog::on_pushButton_4_clicked()
{
QMessageBox::information(this,QObject::tr("密码"),QObject::tr("忘记了也没办法,大哥重新注册一个吧。"),QMessageBox::Ok,QMessageBox::Ok);
}
8.logondialog.cpp
#include "logondialog.h"
#include "ui_logondialog.h"
LogonDialog::LogonDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::LogonDialog)
{
ui->setupUi(this);
}
LogonDialog::~LogonDialog()
{
delete ui;
}
//注册功能实现函数
void LogonDialog::on_pushButton_2_clicked()
{
QString user = ui->lineEdit->text();
QString pwd = ui->lineEdit_2->text();
QString enter_pwd = ui->lineEdit_3->text();
if(user==""||pwd==""||enter_pwd=="")
{
QMessageBox::information(this,QObject::tr("注册提示"),QObject::tr("输入不能为空,请重新输入!"),QMessageBox::Ok,QMessageBox::Ok);
}
else
{
QSqlQuery sql_query=connect_sqlite();
QString logon_sql = QString("select user from users;");
sql_query.exec(logon_sql);
while(sql_query.next())
{
if(sql_query.value(0).toString()==user)
{
QMessageBox::information(this,QObject::tr("注册提示"),QObject::tr("未能注册,账号已存在,请更换账号后重新尝试注册!"),QMessageBox::Ok,QMessageBox::Ok);
}
}
if(pwd!=enter_pwd)
{
QMessageBox::information(this,QObject::tr("注册提示"),QObject::tr("未能注册,两次密码输入不一致,请重新输入!"),QMessageBox::Ok,QMessageBox::Ok);
}
else
{
QString insert = QString("insert into users values('%1','%2');").arg(user).arg(pwd);
if(sql_query.exec(insert))
{
QMessageBox::information(this,QObject::tr("注册提示"),QObject::tr("注册成功!"),QMessageBox::Ok,QMessageBox::Ok);
}
else
{
QMessageBox::information(this,QObject::tr("注册提示"),QObject::tr("未能注册,请尝试重新注册!"),QMessageBox::Ok,QMessageBox::Ok);
}
}
sql_close();
}
}
//关闭注册窗口
void LogonDialog::on_pushButton_clicked()
{
close();
}
9.main.cpp文章来源:https://www.toymoban.com/news/detail-507749.html
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStringList dr = QSqlDatabase::drivers();//查看有哪些数据驱动,原本想用MySQL数据库,但是我qt里没驱动,自己捣鼓了半天也没成功,就直接用sqlite数据库了
foreach(QString driver,dr)
qDebug()<<driver;
QString sql = "create table users(user varchar(40) primary key,passwd varchar(40));";
QString judgmen_table_exist = "select count(*) from users where user = 'z';";
QSqlQuery sql_query1=connect_sqlite();
bool flag = sql_query1.exec(judgmen_table_exist);
if(!flag)
{
flag = sql_query1.exec(sql);
if(flag)
{
qDebug()<<"user table create success";
}
else
{
qDebug()<<"user table create error";
}
}
sql_close();
LoginDialog logindialog;
logindialog.setWindowTitle(QObject::tr("登录"));
if(logindialog.exec()==QDialog::Accepted)
{
Widget w;
w.setWindowTitle(QObject::tr("QtQq"));
w.show();
return a.exec();
}
else
return 0;
}
10.widget.cpp文章来源地址https://www.toymoban.com/news/detail-507749.html
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
//重新登录功能实现函数
void Widget::on_pushButton_2_clicked()
{
close();
LoginDialog ldg;
if(ldg.exec()==QDialog::Accepted)
show();
}
到了这里,关于QT 简单的登录界面的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!