亲宝软件园·资讯

展开

QT用户登录注册

一个有头发的程序猿 人气:0

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

运行结果

加载全部内容

相关教程
猜你喜欢
用户评论