I'm new to qt programming.so please go easy on me. I have one dialog designed in Qt5.4, A modal main Window or a dialog is to be opened when a button is pressed.but i can not find the setModal object to set the Qwindow and when i press the button the dialog dose not hid and neither the Qwindow or dialog will open. here are my codes
Login.h
#ifndef LOGIN_H
#define LOGIN_H
#include <QDialog>
#include <QCoreApplication>
#include <QApplication>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlQuery>
#include <QDebug>
#include "next_page.h"
#include "main_page.h"
namespace Ui {
class Login;
}
{
Q_OBJECT
public:
// function for database connetion
/*
void CloseConncetion()
{
MyDB.close();
MyDB.removeDatabase(QSqlDatabase::defaultConnection);
}
*/
void CloseConncetion()
{
connection = MyDB.connectionName();
MyDB.removeDatabase(connection);
}
bool OpenConncetion(){
MyDB .setHostName("localhost");
MyDB.setDatabaseName("project");
MyDB.setUserName("lord-ivan");
MyDB.setPassword("Ir0ngo@t");
if (!MyDB.open())
{
qDebug()<<("Datebase not Connected");
return false;
}
else
{
qDebug()<<("Datebase Connected");
return true;
}
}
public:
explicit Login
(QWidget *parent
= 0);
~Login();
private slots:
void on_pushButton_Login_clicked();
void on_pushButton_clicked();
private:
Ui::Login *ui;
};
#endif // LOGIN_H
#ifndef LOGIN_H
#define LOGIN_H
#include <QDialog>
#include <QCoreApplication>
#include <QApplication>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QtSql/QSqlQuery>
#include <QDebug>
#include "next_page.h"
#include "main_page.h"
namespace Ui {
class Login;
}
class Login : public QDialog
{
Q_OBJECT
public:
// function for database connetion
QSqlDatabase MyDB; //object for connection
/*
void CloseConncetion()
{
MyDB.close();
MyDB.removeDatabase(QSqlDatabase::defaultConnection);
}
*/
void CloseConncetion()
{
QString connection;
connection = MyDB.connectionName();
MyDB=QSqlDatabase();
MyDB.removeDatabase(connection);
}
bool OpenConncetion(){
QSqlDatabase MyDB = QSqlDatabase::addDatabase("QMYSQL");
MyDB .setHostName("localhost");
MyDB.setDatabaseName("project");
MyDB.setUserName("lord-ivan");
MyDB.setPassword("Ir0ngo@t");
if (!MyDB.open())
{
qDebug()<<("Datebase not Connected");
return false;
}
else
{
qDebug()<<("Datebase Connected");
return true;
}
}
public:
explicit Login(QWidget *parent = 0);
~Login();
private slots:
void on_pushButton_Login_clicked();
void on_pushButton_clicked();
private:
Ui::Login *ui;
};
#endif // LOGIN_H
To copy to clipboard, switch view to plain text mode
Login.cpp
#include "login.h"
#include "ui_login.h"
ui(new Ui::Login)
{
ui->setupUi(this);
if (!OpenConncetion())
ui->label_Status->setText("Datebase not Connected");
else
ui->label_Status->setText("Datebase Connected");
}
Login::~Login()
{
delete ui;
}
void Login::on_pushButton_Login_clicked()
{
username=ui->lineEdit_UserName->text();
password=ui->lineEdit_PassWord->text();
if(!OpenConncetion())
{
qDebug()<<"Datebase not Connected";
return;
}
if(query.exec("SELECT * FROM Users where UserName ='"+username+"' and Password ='"+password+"'")) // query for input
{
int count=0;
while(query.next())
{
count++;
}
if (count==1)
{
ui->label_Status->setText("Username and password is correct");
CloseConncetion(); // it is important to call this funcion to open the connection.
this->hide();
Next_Page page;
page.setModal(true);
page.exec();
}
if (count>1)
ui->label_Status->setText("Duplicate Username and password is correct");
if (count<1)
ui->label_Status->setText("Username and password is not correct");
}
}
void Login::on_pushButton_clicked()
{
username=ui->lineEdit_UserName->text();
password=ui->lineEdit_PassWord->text();
if(!OpenConncetion())
{
qDebug()<<"Datebase not Connected";
return;
}
if(query.exec("SELECT * FROM Users where UserName ='"+username+"' and Password ='"+password+"'")) // query for input
{
int count=0;
while(query.next())
{
count++;
}
if (count==1)
{
ui->label_Status->setText("Username and password is correct");
CloseConncetion(); // it is important to call this funcion to open the connection.
this->hide();
Main_Page Mpage;
Mpage.window();
Mpage.show();
}
if (count>1)
ui->label_Status->setText("Duplicate Username and password is correct");
if (count<1)
ui->label_Status->setText("Username and password is not correct");
}
}
#include "login.h"
#include "ui_login.h"
Login::Login(QWidget *parent) :
QDialog(parent),
ui(new Ui::Login)
{
ui->setupUi(this);
if (!OpenConncetion())
ui->label_Status->setText("Datebase not Connected");
else
ui->label_Status->setText("Datebase Connected");
}
Login::~Login()
{
delete ui;
}
void Login::on_pushButton_Login_clicked()
{
QString username,password;
username=ui->lineEdit_UserName->text();
password=ui->lineEdit_PassWord->text();
if(!OpenConncetion())
{
qDebug()<<"Datebase not Connected";
return;
}
QSqlQuery query;
if(query.exec("SELECT * FROM Users where UserName ='"+username+"' and Password ='"+password+"'")) // query for input
{
int count=0;
while(query.next())
{
count++;
}
if (count==1)
{
ui->label_Status->setText("Username and password is correct");
CloseConncetion(); // it is important to call this funcion to open the connection.
this->hide();
Next_Page page;
page.setModal(true);
page.exec();
}
if (count>1)
ui->label_Status->setText("Duplicate Username and password is correct");
if (count<1)
ui->label_Status->setText("Username and password is not correct");
}
}
void Login::on_pushButton_clicked()
{
QString username,password;
username=ui->lineEdit_UserName->text();
password=ui->lineEdit_PassWord->text();
if(!OpenConncetion())
{
qDebug()<<"Datebase not Connected";
return;
}
QSqlQuery query;
if(query.exec("SELECT * FROM Users where UserName ='"+username+"' and Password ='"+password+"'")) // query for input
{
int count=0;
while(query.next())
{
count++;
}
if (count==1)
{
ui->label_Status->setText("Username and password is correct");
CloseConncetion(); // it is important to call this funcion to open the connection.
this->hide();
Main_Page Mpage;
Mpage.window();
Mpage.show();
}
if (count>1)
ui->label_Status->setText("Duplicate Username and password is correct");
if (count<1)
ui->label_Status->setText("Username and password is not correct");
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks