Open new dialog from a dialog in qt

Tanmay J Shetty picture Tanmay J Shetty · Apr 13, 2012 · Viewed 34k times · Source

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???

Answer

Anthony picture Anthony · Apr 13, 2012

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.