1. create a qt design form class
File->New File or Project->Files and Classes(QT)->Qt Designer Form Class
Form Template(Dialog without Buttons)
Class name(EmployeeInfo)
2. modify login.h
#ifndef LOGIN_H
#define LOGIN_H
#include <QMainWindow>
#include <QtSql>
#include <QDebug>
#include <QFileInfo>
#include "employeeinfo.h"
namespace Ui {
class login;
}
class login : public QMainWindow
{
Q_OBJECT
public:
explicit login(QWidget *parent = 0);
~login();
private slots:
void on_pushButton_clicked();
private:
Ui::login *ui;
QSqlDatabase mydb;
};
#endif // LOGIN_H
3. modify login.cpp
#include "login.h"
#include "ui_login.h"
login::login(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::login)
{
ui->setupUi(this);
QPixmap pix("C:/TestQT/QT_SQLite/Tutorial7_How_to_connect_Qt_to_SQLite/Sqlite_DB/icon/panda.png");
ui->label_pic->setPixmap(pix);
mydb = QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("C:/work_files/sqlite-tools-win32-x86-3110100/company.db");
if(!mydb.open())
ui->label->setText("Failed to open the database");
else
ui->label->setText("Connected...");
}
login::~login()
{
delete ui;
}
void login::on_pushButton_clicked()
{
QString username, password;
username = ui->lineEdit_Username->text();
password = ui->lineEdit_Password->text();
if(!mydb.isOpen())
{
qDebug() << "Failed to open the database";
return ;
}
QSqlQuery qry(mydb);
QString stmt = "select * from employeeinfo where username='"+ username +"' and password='"+ password +"'";
if( qry.exec(stmt))
{
int count = 0;
while(qry.next())
count++;
if( count == 1 )
{
ui->label->setText("username and password is correct");
//this part is new added
this->hide();
EmployeeInfo employeeinfo;
employeeinfo.setModal(true);
employeeinfo.exec();
}
else if( count > 1 )
ui->label->setText("Duplicate username and password");
if( count < 1 )
ui->label->setText("username and password is not correct");
}
else
{
qDebug() << qry.lastError();
}
}