PDA

View Full Version : How would I open a new window from a button in the dialog using Qt Creator?



atoivan
25th January 2015, 19:28
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;
}

class Login : public QDialog
{
Q_OBJECT

public:
// function for database connetion
QSqlDatabase MyDB; //object for connection
/*
void CloseConncetion()
{
MyDB.close();
MyDB.removeDatabase(QSqlDatabase::defaultConnectio n);
}
*/
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



Login.cpp



#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");


}
}

anda_skoa
26th January 2015, 09:45
You usually don't need to set the modality of a dialog.
show() brings it up as a non-modal dialog and exec() brings it up as modal.

From a quick look your Next_Page window should open, but of course Main_Page will not (Mpage doesn't live long enough).

Cheers,
_