Qt - 禁用QDialog的"?"按钮

22

我创建了一个QDialog实例,在'x'(关闭)按钮的左侧还有一个'?'按钮。我该如何禁用这个'?'按钮?

我如何禁用对话框中的“?”按钮?
4个回答

43

更改窗口标志,例如在构造函数中:

this->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);

1
尝试直接调用setWindowFlags()并没有起作用,因为'~'没有被应用。另一个解决方案涉及使用&=运算符:<pre> Qt :: WindowFlags flags = windowFlags(); flags &= ~Qt :: WindowContextHelpButtonHint; setWindowFlags(flags); </pre> - Elias Bachaalany
1
实际上,我们遇到了相反的问题,我们的对话框没有帮助按钮。这个答案帮助我们找到了问题所在。谢谢。 - Liz
顺便提一下,在PySide中:self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint)。这个波浪号是表示要移除该标志吗? - gseattle
Tilde是一种按位补码运算符,它会翻转每个位(0->1, 1->0)。&是一种按位AND运算符。当原始位和翻转标志位进行AND运算时,标志位被移除。 - user362638

5

来自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 the QWidget constructor. If, for example, you don't want a **What's This button in the title bar of the dialog**, pass Qt::WindowTitleHint | Qt::WindowSystemMenuHint in f.

请参考QWidget::setWindowFlags()


2
对于QDialog,Qt :: WindowTitleHint | Qt :: WindowSystemMenuHint标志会导致异常行为。 - Narek

0

如果您只想禁用按钮,可以调用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);

如果您不知道按钮框,我猜设计师已经自动为您添加了它,并且应该有一个易于访问的名称。此外,还应该有属性(复选框),您可以勾选以控制默认情况下哪些按钮是可访问的。

1
这是一篇写得很好的答案,但我认为他指的是对话框标题栏中的按钮(窗口装饰)。 - Caleb Huitt - cjhuitt
哦,我想你是对的——我肯定误解了那个 :(. - Kaleb Pederson

0

对于Qt 5.10及更高版本,您可以使用应用程序范围的标志Qt::AA_DisableWindowContextHelpButton

 app.setAttribute(Qt::AA_DisableWindowContextHelpButton);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接