I want to make a simple 'About' modal dialog, called from Help->About application menu. I've created a modal dialog window with QT Creator (.ui file).
What code should be in menu 'About' slot?
Now I have this code, but it shows up a new modal dialog (not based on my about.ui):
void MainWindow::on_actionAbout_triggered()
{
about = new QDialog(0,0);
about->show();
}
Thanks!
You need to setup the dialog with the UI you from your
.ui
file. The Qtuic
compiler generates a header file from your.ui
file which you need to include in your code. Assumed that your.ui
file is calledabout.ui
, and the Dialog is namedAbout
, thenuic
creates the fileui_about.h
, containing a classUi_About
. There are different approaches to setup your UI, at simplest you can doA better approach is to use inheritance, since it encapsulates your dialogs better, so that you can implement any functionality specific to the particular dialog within the sub class:
AboutDialog.h:
AboutDialog.cpp:
Usage:
In any case, the important code is to call the
setupUi()
method.BTW: Your dialog in the code above is non-modal. To show a modal dialog, either set the
windowModality
flag of your dialog toQt::ApplicationModal
or useexec()
instead ofshow()
.For modal dialogs, you should use
exec()
method of QDialogs.Docs say:
Alternative way: You don't need a modal dialog. Let the dialog show modeless and connect its
accepted()
andrejected()
signals to appropriate slots. Then you can put all your code in the accept slot instead of putting them right aftershow()
. So, using this way, you wouldn't actually need a modal dialog.