QT实现登陆注册界面并于Mysql相连接

Login.h

#ifndef LOGIN_H
#define LOGIN_H

#include 
#include 
#include 
#include 
#include 
#include 

class Loginwindow : public QDialog
{
    Q_OBJECT

public:
    Loginwindow(QWidget *parent = 0);
    ~Loginwindow();
private:
    //声明布局
   QGridLayout *MessageLayout;
   QGridLayout *ButtonLayout;
   QGridLayout *MainLayout;
    //声明信息成员
   QLabel *UserName;
   QLabel *Password;
   QLineEdit *UserNameEdit;
   QLineEdit *PasswordEdit;
   QPushButton *LoginButton;
   QPushButton *RegisterButton;
    //声明槽
public slots:
   void Login_slots();
   void Register_slots();
};

#endif // DIALOG_

Register.h

#ifndef REGISTER_H
#define REGISTER_H
#include 
#include 
#include 
#include 
#include 
#include 

class registerwindow:public QDialog
{
    Q_OBJECT

public:
    registerwindow(QWidget *parent = 0);
    ~registerwindow();
private:
    //声明布局
    QGridLayout *MessageLayout;
    QGridLayout *ButtonLayout;
    QGridLayout *MainLayout;
    //声明信息成员
    QLabel *UserName;
    QLabel *Password;
    QLabel *RePassword;
    QLineEdit *UserNameEdit;
    QLineEdit *PasswordEdit;
    QLineEdit *RePasswordEdit;
    QLabel *Email;
    QLineEdit *EmailEdit;
    QPushButton *confirm;
    QPushButton *resetting;
    //声明槽
public slots:
    void confirm_slots();

};
#endif // REGISTER_H

Login.cpp

#include "Login.h"
#include 
#include 
#include 
#include "Register.h"
#include 
Loginwindow::Loginwindow(QWidget *parent)
    : QDialog(parent)
{
    //登陆界面定义
    setWindowTitle(tr("登陆界面"));
    //窗口只能最小化
    this->setWindowFlags (this->windowFlags () | Qt::WindowMinimizeButtonHint|Qt::MSWindowsFixedSizeDialogHint);
    //成员初始化
    UserName = new QLabel(tr("用户名:"));
    Password = new QLabel(tr("密码:"));
    UserNameEdit = new QLineEdit();
    PasswordEdit = new QLineEdit();
    LoginButton = new QPushButton(tr("登陆"));
    RegisterButton = new QPushButton(tr("注册"));
    //实现上方信息网格布局
    MessageLayout = new QGridLayout();
    MessageLayout->addWidget(UserName,0,0);
    MessageLayout->addWidget(UserNameEdit,0,1);
    MessageLayout->addWidget(Password,1,0);
    MessageLayout->addWidget(PasswordEdit,1,1);
    //实现下方按钮网格布局
    ButtonLayout = new QGridLayout();
    ButtonLayout->addWidget(LoginButton,0,0);
    ButtonLayout->addWidget(RegisterButton,0,1);
    ButtonLayout->setSpacing(15);
    //实现主网格
    MainLayout = new QGridLayout(this);
    MainLayout->addLayout(MessageLayout,0,0);
    MainLayout->addLayout(ButtonLayout,1,0);
    MainLayout->setMargin(15);
    MainLayout->setSpacing(10);
    //连接信号和槽
    connect(LoginButton,SIGNAL(clicked()),this,SLOT(Login_slots()));
    connect(RegisterButton,SIGNAL(clicked()),this,SLOT(Register_slots()));

}
//槽函数实现
void Loginwindow::Login_slots()
{
        QString  UName = UserNameEdit->text();
        QString  Pword = PasswordEdit->text();
        QString s=QString("select * from test_qt_01.user where username='%1'and password='%2' ").arg(UName).arg(Pword);//数据库存储用户名用username密码用password
        /*if(UName == "admin"&&Pword == "admin")
            QMessageBox::information(NULL, "登陆成功", "登陆成功!!!", QMessageBox::Yes);*/
        //查询数据库如果账号和密码匹配返回真否则返回假
        QSqlQuery query;
        query.exec(s);
        if(query.first())
            QMessageBox::information(NULL, "登陆成功", "登陆成功!!!", QMessageBox::Yes);
        else
            QMessageBox::warning(NULL,"Error","登录失败,请重试!!!");

}
void Loginwindow::Register_slots()
{
    this->hide();
    registerwindow re;
    re.show();
    re.exec();
    this->show();
}
Loginwindow::~Loginwindow()
{

}

Register.h

#include "Register.h"
#include 
#include 
#include 
#include 

registerwindow::registerwindow(QWidget *parent)
    :QDialog(parent)
{
    //登陆界面定义
    setWindowTitle(tr("注册界面"));
    //窗口只能最小化
    this->setWindowFlags (this->windowFlags () | Qt::WindowMinimizeButtonHint|Qt::MSWindowsFixedSizeDialogHint);
    //成员初始化
    UserName = new QLabel(tr("用户名:"));
    Password = new QLabel(tr("密码:"));
    RePassword = new QLabel(tr("重复密码:"));
    UserNameEdit = new QLineEdit();
    PasswordEdit = new QLineEdit();
    RePasswordEdit = new QLineEdit();
    Email = new QLabel("邮件:");
    EmailEdit = new QLineEdit();
    confirm = new QPushButton(tr("确认"));
    resetting = new QPushButton(tr("重置"));
    //实现上方信息网格布局
    MessageLayout = new QGridLayout();
    MessageLayout->addWidget(UserName,0,0);
    MessageLayout->addWidget(UserNameEdit,0,1);
    MessageLayout->addWidget(Password,1,0);
    MessageLayout->addWidget(PasswordEdit,1,1);
    MessageLayout->addWidget(RePassword,2,0);
    MessageLayout->addWidget(RePasswordEdit,2,1);
    MessageLayout->addWidget(Email,3,0);
    MessageLayout->addWidget(EmailEdit,3,1);
     //实现下方按钮网格布局
    ButtonLayout = new QGridLayout();
    ButtonLayout->addWidget(confirm,0,0);
    ButtonLayout->addWidget(resetting,0,1);
    ButtonLayout->setSpacing(15);
    //实现主网格
    MainLayout = new QGridLayout(this);
    MainLayout->addLayout(MessageLayout,0,0);
    MainLayout->addLayout(ButtonLayout,1,0);
    MainLayout->setMargin(15);
    MainLayout->setSpacing(10);
    //连接信号和槽
    connect(confirm,SIGNAL(clicked()),this,SLOT(confirm_slots()));
    connect(resetting,SIGNAL(clicked()),UserNameEdit,SLOT(clear()));
    connect(resetting,SIGNAL(clicked()),PasswordEdit,SLOT(clear()));
    connect(resetting,SIGNAL(clicked()),RePasswordEdit,SLOT(clear()));
}
//槽函数实现
void registerwindow::confirm_slots()
{
    QString UName = UserNameEdit->text();
    QString email = EmailEdit->text();
    QString Pword;
    if(PasswordEdit->text()==RePasswordEdit->text())
    {
        Pword = PasswordEdit->text();
        QString i=QString("insert into test_qt_01.user values ('%1','%2','%3'); ").arg(UName).arg(email).arg(Pword);
        QString S =QString("select * from test_qt_01.user where username='%1' ").arg(UName);
        QSqlQuery query;
        if(query.exec(i))
            QMessageBox::information(NULL, "注册成功", "注册成功!!!", QMessageBox::Yes);
        else if(query.exec(S)&&query.first())
            QMessageBox::warning(NULL,"Error","用户名重复,请重试!!!");
        else
            QMessageBox::warning(NULL,"Error","注册失败,请重试!!!");
        this->close();
    }
    else
        QMessageBox::warning(NULL,"Error","密码不重复,请重试!!!");
}

registerwindow::~registerwindow()
{

}

main.h

#include "Login.h"
#include 
#include 
#include 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Loginwindow w;
    //连接数据库
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("test_qt_01");
    db.setUserName("root");
    db.setPort(3306);
    db.setPassword("root1");
    //如果数据库连接成功打开窗口否则退出程序
    if(db.open())
    {
        w.show();
    }
    else
    {
        QMessageBox::warning(NULL,"error","数据库连接失败");
        return 0;
    }
    return a.exec();
}

xxx.pro中加入qt += sql

QT版本5.9.3,Mysql版本8.0.11;

需要注意的是qt连接mysql的时候,要将
libmysql.dll和libmysql.lib拷贝至X:\Qt\Qt5.9.3\Tools\mingw530_32\bin 目录下,而且重要的是这两个需要的是32位的,用64位与编译器不符合会报错。

你可能感兴趣的:(qt)