0

I want to display a dialog at startup of my program, For this purpose I create a new Qt Designer Form Class called Dialog. In main.cpp I have the following code which executes the Dialog class:

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

   QApplication a(argc, argv);

    Dialog diag  ;
    diag.show() ;

   return a.exec();
}

The Dialog class contains an "OK" button. When user pushes that button the Dialog Window close and the MainWindow opens. For this I code it like this:

void Dialog::on_ok_butt_clicked()
{

   MainWindow w ;
   w.show() ;
}

but the MainWindow window not displaying... how can I solve this problem?

10
  • Change MainWindow w ; w.show(); to MainWindow* w= new MainWindow; w->show(); Commented Jan 31, 2018 at 18:08
  • hi ... tnx for the reply ... can you tell me the difference Commented Jan 31, 2018 at 18:12
  • A local variable is destroyed when the function ends, so your window will be deleted. On the other hand if you store the position of the memory it will remain, but your duty is that when you do not need it you will have to eliminate it. Commented Jan 31, 2018 at 18:14
  • can i declare it in the main or in some header extern way for the parent child purposes and in the ok_push_buuton slot just call show()? as the Kanan proposed is it not well to handle it this way ? Commented Jan 31, 2018 at 18:16
  • 1
    @eyllanesc, no problem. most qt new programmers are not aware of good design practices .. thats why I did not include code in my answer .. Commented Jan 31, 2018 at 18:24

2 Answers 2

2

Your problem in the lifetime of objects. In on_ok_butt_clicked() you create MainWindow object. And after you return from on_ok_butt_clicked() MainWindow object will be destroyed.

You need to create an object so that it remains after exiting on_ok_butt_clicked() method. Another option is to stop the execution of the method when displaying dialog (this is unapplicable to QMainWindow, because QMainWindow inherits from QWidget, not from QDialog). First solution:

MainWindow *w = new MainWindow(this);
w->show();

Second solution:

SomeDialog w;
w.exec();
Sign up to request clarification or add additional context in comments.

3 Comments

MainWindow does not have the exec method.
Oh, I was wrong. MainWindow inherits from QWidget, not from QDialog.
Edit your question and improve it.
0

In your way the dialog object is the parent of your mainwindow, thats not good design because dialog is usually a temporary object while mainwindow will be your main UI, so I prefer to create the dialog inside your mainwindow, when you want to show the dialog hide() the mainwindow first and dont forget to either accept() or reject() your dialog

1 Comment

well ... i test this to ... but when i add the following Dialog diag ; diag.show() nothing happens neither

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.