QT12 How to open a new window after successful Login
2016-03-30 01:58
501 查看
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();
}
}
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();
}
}
相关文章推荐
- QT11 How to set image with QLabel in Qt
- QT 简单编写贪吃蛇
- QT7 How to connect Qt to SQLite
- Qt提取EXE文件的大图标
- 关于QT5 Failed to find or load platform plugin "windows" 问题
- QT隐藏鼠标
- Qt编译qsqlmysql.dll插件
- qt5.5.1的程序发布方法
- 在Ubuntu 10.04中文版下Qt编程,使用unixODBC和FreeTDS连接MS SQL Server 2005,并且中文不出现乱码的方法
- Qt自定义委托在QTableView中绘制控件、图片、文字(内容比较全)
- 跨平台开发之qt及开源组件qml material
- 分治——sqtx
- QT5读取中文乱码问题
- Qt 写bmp文件
- PyQt5(4)——控件事件响应与信号槽
- GDKOI2016Day2 T2 QT与泰剧 解题报告
- PyQt5应用与实践
- Qt5布局管理(一)——QSplitter分割窗口类
- Qt error : "No Target Architecture"
- QT5 动态链接库的创建和使用