最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

QT實(shí)現(xiàn)用戶登錄注冊功能

 更新時(shí)間:2022年06月14日 17:10:39   作者:一個(gè)有頭發(fā)的程序猿  
這篇文章主要為大家詳細(xì)介紹了QT實(shí)現(xiàn)用戶登錄注冊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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,"警告","用戶名或密碼錯(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,"警告","注冊成功!",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ù)庫,做一個(gè)基本的sql語句操作

? ? //1、對qt下數(shù)據(jù)庫的驅(qū)動(dòng)進(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)文章

最新評論

东平县| 堆龙德庆县| 嘉义市| 庆元县| 广水市| 红安县| 洛隆县| 德格县| 称多县| 三穗县| 盈江县| 韩城市| 当阳市| 获嘉县| 安溪县| 甘泉县| 微山县| SHOW| 平邑县| 阿合奇县| 井陉县| 济宁市| 长白| 即墨市| 西藏| 株洲市| 乐山市| 镇远县| 金寨县| 元江| 仲巴县| 建瓯市| 隆昌县| 肃南| 陇川县| 若尔盖县| 阜新| 衡阳市| 哈巴河县| 梅州市| 大方县|