Create second window/form/dialog


Add New.. Qt – Qt Designer Form Class
Need to include newform.h in mainwindow.cpp file where has slot to open this new window;
Add "extern newDialog *newdialog" in mainwindow.cpp;
"newDialog *newdialog" in newwindow.cpp;

Then, in click-button which open new window slot, add:

void MainWindow::on_pushButton_clicked()
   if (!newdialog)

=  new newDialog (NULL);

The above method creates dialog in heap,  so when i click close(), i don't delete it from memory. To close dialog or window completely, the code needs to be changed as:

void MainWindow::on_pushButton_clicked()
     newDialog newdialog(NULL);

we can not use:;

show() does not block, so you'll only see a quick flash of your dialog, as it'll be destroyed immediately after calling show() ( when exiting the method ). Using exec() blocks until dialog is closed by the user. 

if create new a window/dialog by using code, and did not pre-create window/dialog from “add new.. QT Designer form class”, the new window/dialog may be no shows on the top. This is because that the new window/dialog does not have a parent window/dialog. In order to make sure the new window/dialog shows on the top, use “add new.. QT Designer form class” create a new window/dialog first.

No comments:

Post a Comment