PDA

View Full Version : qdialog hide and show problem



aakashraj
4th June 2016, 14:39
Hey! I have a mainwindow and 2 dialogs(namely dialog2 and dialog3). I need to go to dialog2 from mainwindow while hiding mainwindow and i need to goto dialog3 from dialog2 while hiding dialog2.
Here is my code:


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog2.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
hide();
Dialog2 ptr;
ptr.setModal(true);
ptr.exec();
show();
}



#include "dialog2.h"
#include "ui_dialog2.h"
#include "dialog3.h"
#include <QDebug>

Dialog2::Dialog2(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog2)
{
ui->setupUi(this);
}

Dialog2::~Dialog2()
{
delete ui;
}

void Dialog2::on_pushButton_clicked()
{
hide();
Dialog3 ptr;
ptr.setModal(true);
ptr.exec();
show();


}


#include "dialog3.h"
#include "ui_dialog3.h"

Dialog3::Dialog3(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog3)
{
ui->setupUi(this);
}

Dialog3::~Dialog3()
{
delete ui;
}

PROBLEM:
When I close dialog3 , dialog2 is not showing up(it's destructor is being called) and mainwindow shows up. Help me out.

d_stranz
4th June 2016, 23:31
Please show that code that makes the connections between the pushbuttons and the slots in the dialogs, and also the code that shows what happens to close the dialogs (i.e. what causes them to return from exec()). As far as I can see, the code you have posted will not show the behavior you observe.

The main issue I see is that you are creating the dialogs without parents, which makes them top-level widgets and not children of the main window or dialog that created them. Because they are parentless, they could have independent lifetimes. Try creating dialog2 as a child of main window, and dialog3 as a child of dialog2 and see if the behavior changes.