QT實(shí)現(xiàn)用戶登錄注冊功能
本文實(shí)例為大家分享了QT實(shí)現(xiàn)用戶登錄注冊的具體代碼,供大家參考,具體內(nèi)容如下
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()) ? ? ? ? { ? ? ? ? ? ? //結(jié)果集為空 ? ? ? ? ? ? //執(zhí)行某操作 ? ? ? ? ? ? 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、數(shù)據(jù)庫連接代碼
#ifndef CONNECTION #define CONNECTION #include <QSqlDatabase> #include <QStringList> #include <QString> #include <QDebug> #include <QSqlQuery> #include <QMessageBox> static bool createConnection() { ? ? //測試用例:連接mysql數(shù)據(jù)庫,做一個基本的sql語句操作 ? ? //1、對qt下數(shù)據(jù)庫的驅(qū)動進(jìn)行遍歷查看 ? ? QStringList drivers = QSqlDatabase::drivers(); ? ? foreach (QString driver, drivers) { ? ? ? ? qDebug()<<drivers; ? ? } ? ? //2、打開數(shù)據(jù)庫過程 ? ? QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); ? ? //數(shù)據(jù)庫連接的信息進(jìn)行配置 ? ? db.setHostName("localhost");//設(shè)置主機(jī)名(數(shù)據(jù)庫所在電腦的名稱) ? ? db.setDatabaseName("medical_system");//設(shè)置數(shù)據(jù)庫名稱 ? ? db.setUserName("root"); ? ? db.setPassword("123456"); ? ? //db.setPort(3306);//因?yàn)槭潜緳C(jī),該段代碼可省略 ? ? if(!db.open()){ ? ? ? ? //打開失敗的情況 ? ? ? ? qDebug()<<"Failed to connect"; ? ? ? ? //實(shí)際情況下我們應(yīng)該使用圖形化窗口提示打開失敗 ? ? ? ? QMessageBox::critical(0,"無法打開數(shù)據(jù)庫","無法創(chuàng)建",QMessageBox::Yes); ? ? ? ? return false; ? ? } ? ? return true; } #endif // CONNECTION
運(yùn)行結(jié)果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Qt OpenCV實(shí)現(xiàn)圖像數(shù)據(jù)采集軟件
這篇文章主要為大家詳細(xì)介紹了如何利用Qt+OpenCV實(shí)現(xiàn)圖像數(shù)據(jù)采集軟件,文中的示例代碼講解詳細(xì),對我學(xué)習(xí)或工作有一定參考價值,感興趣的可以了解一下2022-07-07C++構(gòu)造函數(shù)的類型,淺拷貝與深拷貝詳解
這篇文章主要為大家詳細(xì)介紹了C++構(gòu)造函數(shù)的類型,淺拷貝與深拷貝,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03C++實(shí)現(xiàn)反轉(zhuǎn)鏈表的兩種方法
本文主要介紹了C++實(shí)現(xiàn)反轉(zhuǎn)鏈表的兩種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02C++實(shí)現(xiàn)一鍵關(guān)閉桌面的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C++實(shí)現(xiàn)一鍵關(guān)閉桌面的功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解一下2023-07-07