武漢網(wǎng)站優(yōu)化方案網(wǎng)絡(luò)推廣十大平臺(tái)
目錄
實(shí)現(xiàn)使用數(shù)據(jù)庫的登錄注冊(cè)功能
頭文件:
registrwidget.h:
widget.h:
源文件:
registrwidget.c:
widget.h:
效果圖:
思維導(dǎo)圖
實(shí)現(xiàn)使用數(shù)據(jù)庫的登錄注冊(cè)功能
頭文件:
registrwidget.h:
#ifndef REGISTRWIDGET_H
#define REGISTRWIDGET_H#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QSqlDatabase> //數(shù)據(jù)庫管理類
#include <QSqlQuery> //執(zhí)行sql語句的類
#include <QSqlRecord> //記錄數(shù)據(jù)庫記錄的類namespace Ui {
class registrwidget;
}class registrwidget : public QWidget
{Q_OBJECT
public slots:void jumpToRegister_slot();public:explicit registrwidget(QWidget *parent = nullptr);~registrwidget();// void jumpToWid();void cancel_btn_clicked_slot();void register_btn_clicked_slot();private:Ui::registrwidget *ui;QPushButton *register_btn;QPushButton *cancel_btn;QLabel *lab1;QLineEdit *edit1;QLineEdit *edit2;QLineEdit *edit3;QLabel *lab2;QLabel *lab3;QLabel *lab4;QSqlDatabase db; //定義一個(gè)數(shù)據(jù)庫的類對(duì)象//Widget *wid;
};#endif // REGISTRWIDGET_H
widget.h:
#ifndef WIDGET_H
#define WIDGET_H#include "registrwidget.h"
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QObject>
#include <QMessageBox>
#include <QSqlDatabase> //數(shù)據(jù)庫管理類
#include <QSqlQuery> //執(zhí)行sql語句的類
#include <QSqlRecord> //記錄數(shù)據(jù)庫記錄的類QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();signals://void jumpToLogin();void jumpToRegister();public slots:void loginBtn_slot();void cancelBtn_slot();void registerBtn_slot();private:Ui::Widget *ui;QLabel *lab1;QLineEdit *edit1;QLineEdit *edit2;QLabel *lab2;QLabel *lab3;QPushButton *btn1;QPushButton *btn2;QPushButton *btn3;QSqlDatabase db; //定義一個(gè)數(shù)據(jù)庫的類對(duì)象//login *l1;registrwidget *reg_wid;
};
#endif // WIDGET_H
源文件:
registrwidget.c:
#include "registrwidget.h"
#include "ui_registrwidget.h"void registrwidget::jumpToRegister_slot()
{this->show(); //展示自己的界面
}registrwidget::registrwidget(QWidget *parent) :QWidget(parent),ui(new Ui::registrwidget)
{ui->setupUi(this);//判斷自己的數(shù)據(jù)庫對(duì)象中是否包含了要處理的數(shù)據(jù)庫,如果沒有就添加,如果包含就打開if(!db.contains("registeredusr.db")){//添加一個(gè)數(shù)據(jù)庫db = QSqlDatabase::addDatabase("QSQLITE");//設(shè)置數(shù)據(jù)庫的名字db.setDatabaseName("registeredusr.db");}//此時(shí)已經(jīng)有一個(gè)名為registeredusr.db的數(shù)據(jù)庫//打開數(shù)據(jù)庫if(!db.open()){QMessageBox::critical(this, "失敗", "打開失敗");return;}//說明數(shù)據(jù)庫打開成功//需要使用sql語句進(jìn)行創(chuàng)建表的操作//準(zhǔn)備sql語句QString sql("create table if not exists regedusr(" //創(chuàng)建表"usrname varchar(20) primary key," //賬號(hào)"passwd varchar(20))"); //密碼//準(zhǔn)備語句執(zhí)行者QSqlQuery query;//讓語句執(zhí)行者執(zhí)行sql語句if(!query.exec(sql)){QMessageBox::critical(this, "失敗", "創(chuàng)建失敗");return;}//將圖形化界面的名字改成Login screen(登錄界面)this->setWindowTitle("Register screen");//將ui的圖標(biāo)改成想要的this->setWindowIcon(QIcon(":/icon/wodepeizhenshi.png"));//設(shè)置ui界面的大小為合適的大小this->setFixedSize(QSize(400,300));//插入一個(gè)label,它的寬度與ui等寬,高度大約為整個(gè)ui界面高度的4/9lab1 = new QLabel(this);lab1->resize(QSize(400,133));lab1->move(0,0);//label的內(nèi)容要是一張圖片lab1->setPixmap(QPixmap(":/icon/logo.png"));//設(shè)置圖片填充lab1->setScaledContents(true);//插入兩個(gè)行編輯器,第一個(gè)用于輸入賬號(hào)edit1 = new QLineEdit(this);edit1->resize(QSize(240,30));edit1->move(110,145);edit1->setPlaceholderText("賬號(hào)");//第二個(gè)用于輸入密碼edit2 = new QLineEdit(this);edit2->resize(QSize(240,30));edit2->move(edit1->x(),edit1->y()+35);edit2->setPlaceholderText("密碼");//第二個(gè)回顯模式是密碼模式edit2->setEchoMode(QLineEdit::Password);//第三個(gè)用于確認(rèn)密碼edit3 = new QLineEdit(this);edit3->resize(QSize(240,30));edit3->move(edit1->x(),edit2->y()+35);edit3->setPlaceholderText("請(qǐng)確認(rèn)密碼");//第三個(gè)回顯模式也是密碼模式edit3->setEchoMode(QLineEdit::Password);//每個(gè)行編輯器前都要有一個(gè)label,內(nèi)容為圖片lab2 = new QLabel(this);lab2->resize(37,30);lab2->setPixmap(QPixmap(":/icon/userName.jpg"));lab2->setScaledContents(true);lab2->move(edit1->x()-60,edit1->y());lab3 = new QLabel(this);lab3->resize(37,30);lab3->setPixmap(QPixmap(":/icon/passwd.jpg"));lab3->setScaledContents(true);lab3->move(edit2->x()-60,edit2->y());lab4 = new QLabel(this);lab4->resize(37,30);lab4->setPixmap(QPixmap(":/icon/passwd.jpg"));lab4->setScaledContents(true);lab4->move(edit3->x()-60,edit3->y());//實(shí)例化登錄按鈕和退出按鈕register_btn = new QPushButton("注冊(cè)", this);cancel_btn = new QPushButton("取消", this);register_btn->resize(60,30);cancel_btn->resize(60,30);register_btn->move(120, edit3->y()+45);cancel_btn->move(register_btn->x()+120, register_btn->y());//將取消按鈕的按下信號(hào)與對(duì)應(yīng)的槽函數(shù)連接,點(diǎn)擊取消就關(guān)閉注冊(cè)界面connect(cancel_btn, &QPushButton::clicked, this, ®istrwidget::cancel_btn_clicked_slot);//將注冊(cè)按鈕的按下信號(hào)與對(duì)應(yīng)的槽函數(shù)連接,點(diǎn)擊注冊(cè)就進(jìn)行對(duì)應(yīng)的操作connect(register_btn, &QPushButton::clicked, this, ®istrwidget::register_btn_clicked_slot);
}registrwidget::~registrwidget()
{delete ui;
}void registrwidget::cancel_btn_clicked_slot()
{//使用靜態(tài)成員函數(shù)的方式打開一個(gè)對(duì)話框int ret = QMessageBox::question(this,"取消","是否確認(rèn)取消注冊(cè)",QMessageBox::Yes|QMessageBox::No,QMessageBox::No);switch (ret) {case QMessageBox::Yes://確認(rèn)就退出this->close();break;case QMessageBox::No://否就不進(jìn)行任何操作break;default://應(yīng)該不會(huì)有這種可能性break;}
}void registrwidget::register_btn_clicked_slot()
{QString usrname = edit1->text();QString passwd = edit2->text();QString repasswd = edit3->text();//確保每個(gè)容器中都有數(shù)據(jù)if(usrname.isEmpty() ||passwd.isEmpty() || repasswd.isEmpty()){QMessageBox::warning(this, "警告", "請(qǐng)將信息填寫完整");return;}//判斷,如果兩個(gè)密碼輸入框中的密碼不同,就給出消息框提示,并清空密碼框if(passwd != repasswd){QMessageBox::critical(this, "錯(cuò)誤", "請(qǐng)確保兩次密碼都輸入正確");edit2->clear();edit3->clear();return;}//此時(shí),容器中都有數(shù)據(jù),且確認(rèn)密碼正確,判斷用戶名是否存在//準(zhǔn)備sql語句QString sql = QString("select passwd from regedusr where usrname='%1';").arg(usrname);//準(zhǔn)備語句執(zhí)行者QSqlQuery query;//讓語句執(zhí)行者執(zhí)行sql語句if(!query.exec(sql)){QMessageBox::critical(this, "失敗", "注冊(cè)失敗");return;}if(query.next()){QMessageBox::critical(this, "失敗", "用戶名已存在");return;}//此時(shí)可以進(jìn)行插入操作//準(zhǔn)備sql語句sql = QString("insert into regedusr(usrname, passwd)""values('%1', '%2')").arg(usrname).arg(passwd);//讓語句執(zhí)行者執(zhí)行sql語句if(!query.exec(sql)){QMessageBox::critical(this, "失敗", "注冊(cè)失敗");return;}//此時(shí)注冊(cè)成功QMessageBox::information(this, "成功", "注冊(cè)成功");//注冊(cè)成功后自動(dòng)退出注冊(cè)界面this->close();}
widget.h:
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);reg_wid = new registrwidget;//判斷自己的數(shù)據(jù)庫對(duì)象中是否包含了要處理的數(shù)據(jù)庫,如果沒有就添加,如果包含就打開if(!db.contains("registeredusr.db")){//添加一個(gè)數(shù)據(jù)庫db = QSqlDatabase::addDatabase("QSQLITE");//設(shè)置數(shù)據(jù)庫的名字db.setDatabaseName("registeredusr.db");}//此時(shí)已經(jīng)有一個(gè)名為registeredusr.db的數(shù)據(jù)庫//打開數(shù)據(jù)庫if(!db.open()){QMessageBox::critical(this, "失敗", "打開失敗");return;}//說明數(shù)據(jù)庫打開成功//需要使用sql語句進(jìn)行創(chuàng)建表的操作//準(zhǔn)備sql語句QString sql("create table if not exists regedusr(" //創(chuàng)建表"usrname varchar(20) primary key," //賬號(hào)"passwd varchar(20))"); //密碼//準(zhǔn)備語句執(zhí)行者QSqlQuery query;//讓語句執(zhí)行者執(zhí)行sql語句if(!query.exec(sql)){QMessageBox::critical(this, "失敗", "創(chuàng)建失敗");return;}//將圖形化界面的名字改成Login screen(登錄界面)this->setWindowTitle("Login screen");//將ui的圖標(biāo)改成想要的this->setWindowIcon(QIcon(":/icon/wodepeizhenshi.png"));//設(shè)置ui界面的大小為合適的大小this->setFixedSize(QSize(400,300));//插入一個(gè)label,它的寬度與ui等寬,高度大約為整個(gè)ui界面高度的4/9lab1 = new QLabel(this);lab1->resize(QSize(400,133));lab1->move(0,0);//label的內(nèi)容要是一張圖片lab1->setPixmap(QPixmap(":/icon/logo.png"));//設(shè)置圖片填充lab1->setScaledContents(true);//插入兩個(gè)行編輯器,第一個(gè)用于輸入賬號(hào)edit1 = new QLineEdit(this);edit1->resize(QSize(240,40));edit1->move(110,150);edit1->setPlaceholderText("賬號(hào)");//第二個(gè)用于輸入密碼edit2 = new QLineEdit(this);edit2->resize(QSize(240,40));edit2->move(edit1->x(),edit1->y()+55);edit2->setPlaceholderText("密碼");//第二個(gè)回顯模式是密碼模式edit2->setEchoMode(QLineEdit::Password);//每個(gè)行編輯器前都要有一個(gè)label,內(nèi)容為圖片lab2 = new QLabel(this);lab2->resize(50,40);lab2->setPixmap(QPixmap(":/icon/userName.jpg"));lab2->setScaledContents(true);lab2->move(edit1->x()-60,edit1->y());lab3 = new QLabel(this);lab3->resize(50,40);lab3->setPixmap(QPixmap(":/icon/passwd.jpg"));lab3->setScaledContents(true);lab3->move(edit2->x()-60,edit2->y());//要有兩個(gè)pushbutton,一個(gè)是登錄另一個(gè)是取消btn1 = new QPushButton("登錄",this);btn2 = new QPushButton("取消",this);//重設(shè)他們的尺寸btn1->resize(QSize(60,30));btn2->resize(btn1->size());//移動(dòng)他們的位置到合適btn1->move(170,edit2->y()+55);btn2->move(270,edit2->y()+55);//內(nèi)容都會(huì)有一個(gè)圖片btn1->setIcon(QIcon(":/icon/login.png"));btn2->setIcon(QIcon(":/icon/cancel.png"));//實(shí)例化一個(gè)注冊(cè)按鈕btn3 = new QPushButton("注冊(cè)", this);btn3->resize(btn1->size());//移動(dòng)到一個(gè)合適的位置btn3->move(btn1->x()-100,btn1->y());//將登錄按鈕點(diǎn)擊發(fā)信號(hào)與處理函數(shù)連接connect(this->btn1, &QPushButton::clicked, this, &Widget::loginBtn_slot);//將取消按鈕點(diǎn)擊發(fā)信號(hào)與處理函數(shù)連接connect(this->btn2, &QPushButton::clicked, this, &Widget::cancelBtn_slot);//將跳轉(zhuǎn)信號(hào)與registrwidget界面的處理函數(shù)連接connect(this, &Widget::jumpToRegister, reg_wid, ®istrwidget::jumpToRegister_slot);//將注冊(cè)按鈕點(diǎn)擊信號(hào)與跳轉(zhuǎn)向注冊(cè)界面的信號(hào)連接,點(diǎn)擊注冊(cè)按鈕自動(dòng)跳轉(zhuǎn)到注冊(cè)界面connect(this->btn3, &QPushButton::clicked, this, &Widget::registerBtn_slot);}Widget::~Widget()
{delete ui;
}void Widget::loginBtn_slot()
{QString usrname = edit1->text();QString passwd = edit2->text();//準(zhǔn)備sql語句//按照用戶名查找密碼項(xiàng)QString sql = QString("select passwd from regedusr where usrname='%1';").arg(usrname);//準(zhǔn)備語句執(zhí)行者QSqlQuery query;//讓語句執(zhí)行者執(zhí)行sql語句if(!query.exec(sql)){QMessageBox::critical(this, "失敗", "登錄失敗");return;}if(!query.next()){QMessageBox::critical(this, "登錄失敗", "用戶名不存在,請(qǐng)先注冊(cè)");edit2->clear();return;}else{//此時(shí),數(shù)據(jù)表中對(duì)應(yīng)的用戶名是存在對(duì)應(yīng)密碼的if(passwd == query.value(0).toString()){//對(duì)象版實(shí)現(xiàn)打開一個(gè)信息對(duì)話框QMessageBox box(QMessageBox::Information,"登錄成功","登錄成功",QMessageBox::Ok,this);box.exec();}else{//對(duì)象版實(shí)現(xiàn)打開一個(gè)錯(cuò)誤對(duì)話框QMessageBox box(QMessageBox::Critical,"錯(cuò)誤","賬號(hào)密碼不匹配,是否重新登錄",QMessageBox::Ok|QMessageBox::Cancel,this);//設(shè)置默認(rèn)選擇的按鈕box.setDefaultButton(QMessageBox::Ok);int ret = box.exec();//有兩個(gè)按鈕,需要判斷if(QMessageBox::Ok==ret){edit2->clear();}else{this->close();}}}
}void Widget::cancelBtn_slot()
{//使用靜態(tài)成員函數(shù)的方式打開一個(gè)對(duì)話框int ret = QMessageBox::question(this,"請(qǐng)問","是否確認(rèn)要退出",QMessageBox::Yes|QMessageBox::No,QMessageBox::No);switch (ret) {case QMessageBox::Yes://確認(rèn)就退出this->close();break;case QMessageBox::No://否就不進(jìn)行任何操作break;default://應(yīng)該不會(huì)有這種可能性break;}
}void Widget::registerBtn_slot()
{emit jumpToRegister();
}