How would I open a new window from a button in the dialog using Qt Creator?
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
Code:
#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
Login.cpp
Code:
#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");
}
}
Re: How would I open a new window from a button in the dialog using Qt Creator?
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,
_