QT实现用户登录注册功能

本文实例为大家分享了QT实现用户登录注册的具体代码,供大家参考,具体内容如下
1、login.h

#ifndef LOGIN_H#define LOGIN_H#include namespace Ui {class Login; }class Login : public QWidget{Q_OBJECTpublic: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 #include #include #include 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 namespace Ui {class Register; }class Register : public QWidget{Q_OBJECTpublic:explicit Register(QWidget *parent = 0); ~Register(); private slots:void on_btn_logon_clicked(); private:Ui::Register *ui; }; #endif // REGISTER_H

【QT实现用户登录注册功能】4、register.cpp
#include "register.h"#include "ui_register.h"#include #include #include #include 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); //一个值为0bg->addButton(ui->rbtn_female,1); //一个值为1int 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 #include #include #include #include #include static bool createConnection(){//测试用例:连接mysql数据库,做一个基本的sql语句操作//1、对qt下数据库的驱动进行遍历查看QStringList drivers = QSqlDatabase::drivers(); foreach (QString driver, drivers) {qDebug()<
运行结果
QT实现用户登录注册功能
文章图片

QT实现用户登录注册功能
文章图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读