QT實現用戶登錄註冊功能

本文實例為大傢分享瞭QT實現用戶登錄註冊的具體代碼,供大傢參考,具體內容如下

1、login.h

#ifndef LOGIN_H
#define LOGIN_H

#include <QWidget>

namespace Ui {
class Login;
}

class Login : public QWidget
{
    Q_OBJECT

public:
    explicit Login(QWidget *parent = 0);
    ~Login();

private slots:
    void on_btn_login_clicked();
    void on_btn_register_clicked();

private:
    Ui::Login *ui;
};

#endif // WIDGET_H

2、login.cpp

#include "login.h"
#include "ui_login.h"
#include "register.h"
#include "mainwindow.h"
#include <QMessageBox>
#include <QSqlQuery>
#include <QFile>
#include <QDebug>

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    ui->ledit_password->setEchoMode(QLineEdit::Password);
}

Login::~Login()
{
    delete ui;
}

void Login::on_btn_login_clicked()
{
    QString username = ui->ledit_username->text();
    QString password = ui->ledit_password->text();

    if(username == "" ||password == ""){
        QMessageBox::information(this,"警告","輸入不能為空",QMessageBox::Ok);
    }else{

        QSqlQuery query;
        query.prepare("select username,password from admin where username=:username and password = :password ");

        query.bindValue(":username", username);
        query.bindValue(":password", password);
        query.exec();
        if(!query.next())
        {
            //結果集為空
            //執行某操作
            QMessageBox::information(this,"警告","用戶名或密碼錯誤!",QMessageBox::Ok);
        }
        else
        {
            QMessageBox::information(this,"提醒","登錄成功!",QMessageBox::Ok);
            MainWindow *m = new MainWindow;
            m->show();
            this->close();
        }
    }


}

void Login::on_btn_register_clicked()
{
    Register *r = new Register;
    r->show();
}

3、register.h

#ifndef REGISTER_H
#define REGISTER_H

#include <QWidget>

namespace Ui {
class Register;
}

class Register : public QWidget
{
    Q_OBJECT

public:
    explicit Register(QWidget *parent = 0);
    ~Register();

private slots:
    void on_btn_logon_clicked();

private:
    Ui::Register *ui;
};

#endif // REGISTER_H

4、register.cpp

#include "register.h"
#include "ui_register.h"
#include <QButtonGroup>
#include <QMessageBox>
#include <QRegExp>
#include <QSqlQuery>

Register::Register(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Register)
{
    ui->setupUi(this);

}

Register::~Register()
{
    delete ui;
}

void Register::on_btn_logon_clicked()
{
    QString username = ui->ledit_username->text();
    QString password = ui->ledit_pwd->text();
    QString name = ui->ledit_name->text();
    int age = ui->ledit_age->text().toInt();

    QButtonGroup *bg=new QButtonGroup(this);
    bg->addButton(ui->rbtn_male,0);//一個值為0
    bg->addButton(ui->rbtn_female,1);//一個值為1

    int sel=bg->checkedId();//取到你所選的radioButton的值

    QString gender;

    switch(sel)
    {
    case 0:
      gender="男";
      break;
    case 1:
      gender="女";
      break;
    default:
      gender="";
    break;

    }

    QSqlQuery query;
    query.prepare("select username from patient where username=:username");
    query.bindValue(":username", username);
    query.exec();
    if(query.next())
    {

        QMessageBox::information(this,"警告","用戶名已存在!",QMessageBox::Ok);

    }
    else
    {
        query.prepare("insert into patient(username,password,patientName,age,gender)"
                      "values(:username,:password,:patientName,:age,:gender)");
        query.bindValue(":username", username);
        query.bindValue(":password",password);
        query.bindValue(":patientName", name);
        query.bindValue(":age", age);
        query.bindValue(":gender", gender);

        query.exec();
        QMessageBox::information(this,"警告","註冊成功!",QMessageBox::Ok);

    }
}

5、數據庫連接代碼

#ifndef CONNECTION
#define CONNECTION

#include <QSqlDatabase>
#include <QStringList>
#include <QString>
#include <QDebug>
#include <QSqlQuery>
#include <QMessageBox>

static bool createConnection()
{
    //測試用例:連接mysql數據庫,做一個基本的sql語句操作

    //1、對qt下數據庫的驅動進行遍歷查看
    QStringList drivers = QSqlDatabase::drivers();
    foreach (QString driver, drivers) {
        qDebug()<<drivers;

    }

    //2、打開數據庫過程
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    //數據庫連接的信息進行配置
    db.setHostName("localhost");//設置主機名(數據庫所在電腦的名稱)
    db.setDatabaseName("medical_system");//設置數據庫名稱
    db.setUserName("root");
    db.setPassword("123456");
    //db.setPort(3306);//因為是本機,該段代碼可省略

    if(!db.open()){
        //打開失敗的情況
        qDebug()<<"Failed to connect";

        //實際情況下我們應該使用圖形化窗口提示打開失敗
        QMessageBox::critical(0,"無法打開數據庫","無法創建",QMessageBox::Yes);
        return false;
    }

    return true;


}

#endif // CONNECTION

運行結果

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: