C++ Qt在菜单项单击时显示模态对话框(.ui)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13116863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Qt show modal dialog (.ui) on menu item click
提问by Michael Zelensky
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).
我想制作一个简单的“关于”模式对话框,从“帮助”->“关于应用程序”菜单调用。我已经使用 QT Creator(.ui 文件)创建了一个模态对话框窗口。
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):
现在我有了这个代码,但它显示了一个新的模式对话框(不是基于我的 about.ui):
void MainWindow::on_actionAbout_triggered()
{
about = new QDialog(0,0);
about->show();
}
Thanks!
谢谢!
回答by Andreas Fester
You need to setup the dialog with the UI you from your .ui
file. The Qt uic
compiler generates a header file from your .ui
file which you need to include in your code. Assumed that your .ui
file is called about.ui
, and the Dialog is named About
, then uic
creates the file ui_about.h
, containing a class Ui_About
. There are different approaches to setup your UI, at simplest you can do
您需要使用您的.ui
文件中的 UI 设置对话框。Qtuic
编译器从您的.ui
文件生成一个头文件,您需要将其包含在您的代码中。假设你的.ui
文件被调用about.ui
,Dialog 被命名About
,然后uic
创建ui_about.h
包含一个类的文件Ui_About
。有不同的方法来设置你的 UI,最简单的你可以做
#include "ui_about.h"
...
void MainWindow::on_actionAbout_triggered()
{
about = new QDialog(0,0);
Ui_About aboutUi;
aboutUi.setupUi(about);
about->show();
}
A 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:
关于Dialog.h:
#include <QDialog>
#include "ui_about.h"
class AboutDialog : public QDialog, public Ui::About {
Q_OBJECT
public:
AboutDialog( QWidget * parent = 0);
};
AboutDialog.cpp:
关于Dialog.cpp:
AboutDialog::AboutDialog( QWidget * parent) : QDialog(parent) {
setupUi(this);
// perform additional setup here ...
}
Usage:
用法:
#include "AboutDialog.h"
...
void MainWindow::on_actionAbout_triggered() {
about = new AboutDialog(this);
about->show();
}
In any case, the important code is to call the setupUi()
method.
无论如何,重要的代码是调用setupUi()
方法。
BTW:Your dialog in the code above is non-modal. To show a modal dialog, either set the windowModality
flag of your dialog to Qt::ApplicationModal
or use exec()
instead of show()
.
顺便说一句:上面代码中的对话框是非模态的。要显示模态对话框,请将对话框的windowModality
标志设置为Qt::ApplicationModal
或使用exec()
代替show()
。
回答by frogatto
For modal dialogs, you should use exec()
method of QDialogs.
对于模态对话框,您应该使用exec()
QDialogs 的方法。
about = new QDialog(0, 0);
// The method does not return until user closes it.
about->exec();
// In this point, the dialog is closed.
Docssay:
文档说:
The most common way to display a modal dialog is to call its
exec()
function. When the user closes the dialog,exec()
will provide a useful return value.
显示模态对话框最常见的方式是调用它的
exec()
函数。当用户关闭对话框时,exec()
会提供一个有用的返回值。
Alternative way: You don'tneed a modal dialog. Let the dialog show modeless and connect its accepted()
and rejected()
signals to appropriate slots. Then you can put all your code in the acceptslot instead of putting them right after show()
. So, using this way, you wouldn't actually need a modal dialog.
替代方法:你并不需要一个模式对话框。让对话框显示无模式并将其accepted()
和rejected()
信号连接到适当的插槽。然后,您可以将所有代码放在接受槽中,而不是将它们放在show()
. 因此,使用这种方式,您实际上不需要模态对话框。