6

I am trying to open a new dialog Window from a existing dialog on a a button click event,but I am not able to do this as i opened the dialog window from MainWindow.

I am trying with:

Dialog1 *New = new Dialog1();

New->show(); 

Is there a different way of opening dialog window form existing dialog Window???

sashoalm
  • 75,001
  • 122
  • 434
  • 781
Tanmay J Shetty
  • 197
  • 2
  • 3
  • 9

3 Answers3

9

There must be some other problem, because your code looks good to me. Here's how I'd do it:

#include <QtGui>

class Dialog : public QDialog
{
public:
    Dialog()
    {
        QDialog *subDialog = new QDialog;
        subDialog->setWindowTitle("Sub Dialog");
        QPushButton *button = new QPushButton("Push to open new dialog", this);
        connect(button, SIGNAL(clicked()), subDialog, SLOT(show()));
    }
};

class MainWindow : public QMainWindow
{
public:
    MainWindow()
    {
        Dialog *dialog = new Dialog;
        dialog->setWindowTitle("Dialog");
        dialog->show();
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MainWindow w;
    w.setWindowTitle("Main Window");
    w.show();

    return a.exec();
}

By the way, note how I've connected QPushButton's "clicked" signal to QDialog's "show" slot. Very handy.

Anthony
  • 8,570
  • 3
  • 38
  • 46
3

I am new to QT and I did have a similar problem. In my case, I was calling the new dialog from a function from the main dialog. I was using dlg->show which does not wait until the result of the new dialog. Hence the program still running. I change dlg->show for dlg->exec and the dialog works now. In your code, the dialog seems to be a local variable, perhaps you have the same problem. Other option could be to use a static pointer instead.

Dialog1 *newDlg = new Dialog1();
this->hide();
int result = newDlg->exec();
this->show();
delete newDlg;
Antonio
  • 851
  • 2
  • 8
  • 17
1

in mainwindow.h file you should declare a pointer to your new dialog and include the new dialog.h like

#include <myNewDialog.h>

private:
    Ui::MainWindow *ui;
    MyNewDialog *objMyNewDialog;

and after that you can call your dialog to be shown up in mainwindow.cpp like

void MainWindow::on_btnClose_clicked()
{    
    objMyNewDialog= new MyNewDialog(this);
    objMyNewDialog->show();
}
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
Ansary
  • 11
  • 1