我创建了一个QDialog实例,在'x'(关闭)按钮的左侧还有一个'?'按钮。我该如何禁用这个'?'按钮?
我如何禁用对话框中的“?”按钮?更改窗口标志,例如在构造函数中:
this->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);
来自Qt 4.6 QDialog文档:
QDialog::QDialog ( QWidget * parent = 0, Qt::WindowFlags f = 0 )
Constructs a dialog with parent
parent
.A dialog is always a top-level widget, but if it has a parent, its default location is centered on top of the parent. It will also share the parent's taskbar entry.
The widget flags
f
are passed on to theQWidget
constructor. If, for example, you don't want a **What's This button in the title bar of the dialog**, passQt::WindowTitleHint | Qt::WindowSystemMenuHint
inf
.
如果您只想禁用按钮,可以调用setEnabled(bool)
,但我怀疑这不是问题的所在。
如果您想要删除该按钮,请参见以下内容:
QDialog
旨在使用QDialogButtonBox
作为对话框上显示的按钮。 您可以使用QDialogButtonBox中提供的访问器来禁用您不想要的按钮(以及启用其他按钮)。
例如(来自上面链接的文档):
findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);
moreButton = new QPushButton(tr("&More"));
moreButton->setCheckable(true);
moreButton->setAutoDefault(false);
buttonBox = new QDialogButtonBox(Qt::Vertical);
buttonBox->addButton(findButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(moreButton, QDialogButtonBox::ActionRole);
对于Qt 5.10及更高版本,您可以使用应用程序范围的标志Qt::AA_DisableWindowContextHelpButton
app.setAttribute(Qt::AA_DisableWindowContextHelpButton);