I made the following panel with QDialog Window:
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();
}
I made the following panel with QDialog Window:
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();
}
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.
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.