How can I hide/delete the “?” help button on the “title bar” of a Qt Dialog?

喜夏-厌秋 提交于 2019-11-27 13:23:43
amos

By default the Qt::WindowContextHelpButtonHint flag is added to dialogs. You can control this with the WindowFlags parameter to the dialog constructor.

For instance you can specify only the TitleHint and SystemMenu flags by doing:

QDialog *d = new QDialog(0, Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
d->exec();

If you add the Qt::WindowContextHelpButtonHint flag you will get the help button back.

In PyQt you can do:

from PyQt4 import QtGui, QtCore
app = QtGui.QApplication([])
d = QtGui.QDialog(None, QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint)
d.exec_()

More details on window flags can be found on the WindowType enum in the Qt documentation.

// remove question mark from the title bar
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
AMM

Ok , I found a way to do this.

It does deal with the Window flags. So here is the code i used:

Qt::WindowFlags flags = windowFlags()

Qt::WindowFlags helpFlag =
Qt::WindowContextHelpButtonHint;

flags = flags & (~helpFlag);   
setWindowFlags(flags);

But by doing this sometimes the icon of the dialog gets reset. So to make sure the icon of the dialog does not change you can add two lines.

QIcon icon = windowIcon();

Qt::WindowFlags flags = windowFlags();

Qt::WindowFlags helpFlag =
Qt::WindowContextHelpButtonHint;

flags = flags & (~helpFlag);   

setWindowFlags(flags);

setWindowIcon(icon);
Parker Coates

As of Qt 5.10, you can disable these buttons globally with a single QApplication attribute!

QApplication::setAttribute(Qt::AA_DisableWindowContextHelpButton);

I ran into this issue in Windows 7, Qt 5.2, and the flags combination that worked best for me was this:

Qt::WindowTitleHint | Qt::WindowCloseButtonHint

This gives me a working close button but no question mark help button. Using just Qt::WindowTitleHint or Qt::WindowSystemMenuHint got rid of the help button, but it also disabled the close button.

As Michael Bishop suggested, it was playing with the windowflags example that led me to this combination. Thanks!

The answers listed here will work, but to answer it yourself, I'd recommend you run the example program $QTDIR/examples/widgets/windowflags. That will allow you to test all the configurations of window flags and their effects. Very useful for figuring out squirrelly windowflags problems.

The following way to remove question marks by default for all the dialogs in application could be used:

Attach the following event filter to QApplication somewhere at the start of your program:

  bool eventFilter (QObject *watched, QEvent *event) override
  {
    if (event->type () == QEvent::Create)
      {
        if (watched->isWidgetType ())
          {
            auto w = static_cast<QWidget *> (watched);
            w->setWindowFlags (w->windowFlags () & (~Qt::WindowContextHelpButtonHint));
          }
      }
    return QObject::eventFilter (watched, event);
  }
brandoneggar

I couldn't find a slot but you can override the virtual winEvent function.

#if defined(Q_WS_WIN)
bool MyWizard::winEvent(MSG * msg, long * result)
{
    switch (msg->message)
    {
    case WM_NCLBUTTONDOWN:
        if (msg->wParam == HTHELP)
        {

        }
        break;
    default:
        break;
    }
    return QWizard::winEvent(msg, result);
}
#endif
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!