一、描述
有登錄界面、注冊界面,用到sqlite數(shù)據(jù)庫保存賬號和密碼,界面還沒有布局美化等,只實現(xiàn)了最基本的功能。
二、界面及功能介紹
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()
{
//驗證數(shù)據(jù)庫連接是否存在,不存在就創(chuàng)建一個
if(QSqlDatabase::contains("first"))
{
db = QSqlDatabase::database("first");
}
else
{
//用的是sqlite數(shù)據(jù)庫
db = QSqlDatabase::addDatabase("QSQLITE","first");
db.setDatabaseName("QtQq.db");//設置數(shù)據(jù)庫名稱,若不存在自動創(chuàng)建一個
db.setUserName("zhanglinghua");//設置數(shù)據(jù)庫賬號,不用也沒啥問題
db.setPassword("123456"); //設置數(shù)據(jù)庫密碼,不用也沒啥問題
}
bool a = db.open(); //打開數(shù)據(jù)庫,必須打開才能進行增刪查改的操作
if(!a)
{
qDebug()<<"數(shù)據(jù)庫打開失敗";
exit(100);
}
else
{
qDebug()<<"數(shù)據(jù)庫打開成功";
}
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;
}
//驗證登錄函數(shù)
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("未發(fā)現(xiàn)該賬號,請先注冊。"),QMessageBox::Ok,QMessageBox::Ok);
}
}
//關閉注冊窗口
void LoginDialog::on_pushButton_2_clicked()
{
close();
}
//顯示注冊窗口
void LoginDialog::on_pushButton_3_clicked()
{
LogonDialog lgo;
lgo.setWindowTitle("注冊");
lgo.exec();
}
//忘記密碼函數(shù)
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;
}
//注冊功能實現(xiàn)函數(shù)
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文章來源:http://www.zghlxwxcb.cn/news/detail-507749.html
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStringList dr = QSqlDatabase::drivers();//查看有哪些數(shù)據(jù)驅動,原本想用MySQL數(shù)據(jù)庫,但是我qt里沒驅動,自己搗鼓了半天也沒成功,就直接用sqlite數(shù)據(jù)庫了
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文章來源地址http://www.zghlxwxcb.cn/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;
}
//重新登錄功能實現(xiàn)函數(shù)
void Widget::on_pushButton_2_clicked()
{
close();
LoginDialog ldg;
if(ldg.exec()==QDialog::Accepted)
show();
}
到了這里,關于QT 簡單的登錄界面的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!