1

I have one button on MainWindow to show another QMainWindow.

void MainWindow::viewSecondWindow()
{
    QMainWindow w = new QMainWindow(this);
    w.show();
}

The code got compiled well and ran succesfully. However if I click on the button , the second window does pop out but it automatically closes.

Edit :

For the future people who face similiar situation, please refer to this. Why use pointers?

Community
  • 1
  • 1
HohoHaha
  • 25
  • 6
  • Did you mean QMainWindow *w = new QMainWindow(this); actually? – demonplus Sep 25 '15 at 04:47
  • hi @demonplus, that just solved my problem. Thank you so much. I'm very new to c++. If you could write ur answer below I will mark it as accepted. Thanks again. Have a good day. – HohoHaha Sep 25 '15 at 04:59

2 Answers2

0

you can use QDialog in place of Mainwindow

shengfu zou
  • 560
  • 1
  • 7
  • 16
0

You need to use the following instead:

QMainWindow *w = new QMainWindow(this);

Because otherwise w will be destroyed immediately after show().

demonplus
  • 5,613
  • 12
  • 49
  • 68