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