Qt登陆页面的写法

  登陆页面即读取数据库的用户名和密码信息,进行匹配正确后方可下一步操作。本例IDE采用Qt creator,数据库SQLite。

建立数据库、creator工程:

Qt登陆页面的写法_第1张图片Qt登陆页面的写法_第2张图片

主页面、注册页面及其控件变量名:

Qt登陆页面的写法_第3张图片Qt登陆页面的写法_第4张图片

Qt登陆页面的写法_第5张图片Qt登陆页面的写法_第6张图片

 

main.cpp

#include 
#include "mainwindow.h"
// #include "enroll.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include "enrollwindow.h"
using namespace std;

typedef struct{
    string UsrName;
    string Password;
}UsrInfo;

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void changeEvent(QEvent *e);
    void getUsrList();
    void showUsrList();
private:
    Ui::MainWindow *ui;
    EnrollWindow m_penroll;
    QSqlDatabase m_database;
    bool m_bOpen;
    vectorm_usrList;
private slots:
    void on_pushButton_Enroll_clicked();
    void on_pushButton_Cel_clicked();
    void on_pushButton_Login_clicked();
};

#endif // MAINWINDOW_H

enrollwindow.h

#ifndef ENROLLWINDOW_H
#define ENROLLWINDOW_H

#include 
#include 
#include 

namespace Ui {
    class EnrollWindow;
}

class EnrollWindow : public QMainWindow {
    Q_OBJECT
public:
    EnrollWindow(QWidget *parent = 0);
    ~EnrollWindow();

protected:
    void changeEvent(QEvent *e);

private:
    Ui::EnrollWindow *ui;

private slots:
    void on_pushButton_cel_clicked();
    void on_pushButton_enroll_clicked();
};

#endif // ENROLLWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
  //  m_penroll = parent;

    m_database = QSqlDatabase::addDatabase("QSQLITE");
    m_database.setDatabaseName("login.db");
    if(m_database.open())
    {
        m_bOpen = true;

    }
    m_bOpen = false;

    showUsrList();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::showUsrList()
{
    getUsrList();
    int nCount=m_usrList.size();
    for(int i=0;icomboBox->addItem(usr.UsrName.c_str(),i);
    }
    if (nCount > 0)
    {
        ui->comboBox->setCurrentIndex(0);
    }

}

void MainWindow::getUsrList()
{
    QSqlQuery sql_query;
    QString select_all_sql = "select * from main";
    sql_query.prepare(select_all_sql);
    if(!sql_query.exec())
    {
        return;
    }
    else
    {
        while(sql_query.next())
        {
            UsrInfo usr;
            QString name = sql_query.value(0).toString();
            usr.UsrName=name.toStdString();
            QString password = sql_query.value(1).toString();
            usr.Password=password.toStdString();
            m_usrList.push_back(usr);
        }
    }

}

void MainWindow::on_pushButton_Login_clicked()
{
    QString name=ui->comboBox->currentText();
    QString password=ui->lineEdit->text();
    QString sqlPassword;
        QSqlQuery sql_query;
        QString select_all_sql = "select * from main where usr='";
        select_all_sql+=name;
        select_all_sql+="'";
        sql_query.prepare(select_all_sql);
        if(!sql_query.exec())
        {
            return;
        }
        else
        {
            if(!sql_query.next())
            {
                return;
            }
            else
            {
                 sqlPassword = sql_query.value(1).toString();
            }
        }
        if(sqlPassword==password)
        {
            QMessageBox::information(this,QString::fromUtf8("提示"),QString::fromUtf8("登陆成功!"));
        }
        else
        {
            QMessageBox::warning(this,QString::fromUtf8("提示"),QString::fromUtf8("密码错误!"));
        }
}

void MainWindow::on_pushButton_Cel_clicked()
{
    this->close();
}

void MainWindow::on_pushButton_Enroll_clicked()

enrollwindow.cpp

#include "enrollwindow.h"
#include "ui_enrollwindow.h"
#include 
EnrollWindow::EnrollWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::EnrollWindow)
{
    ui->setupUi(this);
}

EnrollWindow::~EnrollWindow()
{
    delete ui;
}

void EnrollWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void EnrollWindow::on_pushButton_enroll_clicked()
{
    QString name = ui->lineEdit_name->text();
    QString password = ui->lineEdit_password->text();
     QString strSql;
     QSqlQuery sql_query;
     strSql = QString("insert into main(usr, password) values ( '%1', '%2');").arg(QString(name)).arg(QString(password));
     sql_query.prepare(strSql);
     if(!sql_query.exec())
     {
         //qDebug()<

登陆成功:

Qt登陆页面的写法_第7张图片

密码错误:

Qt登陆页面的写法_第8张图片

注册成功及其对应SQLite数据库:

Qt登陆页面的写法_第9张图片

Qt登陆页面的写法_第10张图片

你可能感兴趣的:(Qt登陆页面的写法)