0

I made the following panel with QDialog Window:

enter image description here

I want to, I clicked "OK" button view MainWindow.

I wrote the following code but it did not work

void QDialog::pushButton_clicked()
{
    MainWindow w;
    w.show();
}
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
oğuzhan kaynar
  • 77
  • 1
  • 10

2 Answers2

1

If your QApplication exec() is already running, you shold still be able to open a QMainWindow, but, in your example, you create the MainWindow on the stack inside your functions, and it's scope ends right after the call to show().

This means that the MainWindow object will be freed once the pushButton_clicked() function returns.

Tip: Move the declaration of your MainWindow somewhere else, e.g. give it a global scope or move it to the declaration of your own QApplication class, etc.

Steeve
  • 1,323
  • 1
  • 12
  • 23
1

In Addition to Steeves answer you could also change the code to

    MainWindow *w = new MainWindow();
    w->setAttribute(Qt::WA_DeleteOnClose);
    w->show();

The allocation on heap will prevent the direct "freeing" and the WA_DeleteOnClose ensures, that the Memeory is freed when you close the Window.

This is a good way if you want a stand-alone Window for which you do not know the Scope.

Community
  • 1
  • 1
ab.o2c
  • 238
  • 2
  • 7
  • Qdialog window running in a Mainwindow. When this code is written in QDialog, QDialog is opening again. It not open Mainwindow. @ab.o2c – oğuzhan kaynar Dec 13 '16 at 12:27
  • Do you call the Dialog within the Constructor or the show() function of the MainWindow? – ab.o2c Dec 13 '16 at 12:49
  • int main (int argc, char *argv) {QApplication a(argc, argv); QDialog w; w.show(); return a.exec(); } //MainWindow class @ab.o2c – oğuzhan kaynar Dec 13 '16 at 13:01