103

I am using Qt Dialogs in one of my application. I need to hide/delete the help button. But i am not able to locate where exactly I get the handle to his help button. Not sure if its a particular flag on the Qt window.

demonplus
  • 5,613
  • 12
  • 49
  • 68
AMM
  • 17,130
  • 24
  • 65
  • 77

9 Answers9

83
// remove question mark from the title bar
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • 4
    This should be the accepted answer, since it preserves previously set flags. – campovski Nov 18 '20 at 19:42
  • 3
    And if you don't like bit fiddling: `setWindowFlags(windowFlags().setFlag(Qt::WindowContextHelpButtonHint, false))` Which of these two is more readable is pretty subjective, though. – Parker Coates Jan 12 '21 at 14:27
70

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.

feedc0de
  • 3,646
  • 8
  • 30
  • 55
amos
  • 888
  • 9
  • 6
  • 9
    See rrwick's answer below. If you do not add Qt::WindowCloseButtonHint to your flags, you will have disabled the close button, which by default is enabled. – Dan Jan 12 '15 at 21:54
  • 1
    Here is a link specifying window flags in PyQt4, http://pyqt.sourceforge.net/Docs/PyQt4/qt.html#WindowType-enum – Barmaley Jun 09 '15 at 20:43
  • 5
    Also add the flag `Qt::WindowCloseButtonHint` if you want the close button to be activated. – Donald Duck Sep 30 '16 at 16:25
  • 2
    I was able to just set `QtCore.Qt.WindowCloseButtonHint` and it removed `QtCore.Qt.WindowTitleHint`. I assume that setting the flags overrides the default flags, so you need specify all the flags you do want. – artomason Dec 08 '17 at 19:35
  • See also @Parker Coates's reply below for how to remove the '?' globally with a one-liner, from Qt 5.10 onwards – Clare Macrae Dec 08 '18 at 22:30
  • [This](https://stackoverflow.com/a/30934930/929315) is the better answer since it preserves existing flags. – Rotsiser Mho May 04 '21 at 13:15
44

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

QApplication::setAttribute(Qt::AA_DisableWindowContextHelpButton);
Clare Macrae
  • 3,670
  • 2
  • 31
  • 45
Parker Coates
  • 8,520
  • 3
  • 31
  • 37
  • 1
    Very helpful, thanks. However incorrect, if you want to change it on per-dialog basis (not my case though, so I used your solution). – campovski Nov 18 '20 at 19:44
33

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);
demonplus
  • 5,613
  • 12
  • 49
  • 68
AMM
  • 17,130
  • 24
  • 65
  • 77
19

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!

rrwick
  • 619
  • 7
  • 19
  • 1
    This is the right answer. The answers above disable the close button, which by default, is not disabled. – Dan Jan 12 '15 at 21:53
  • 1
    For anyone with PyQT, first initiate the Dialog and then use the following: self.setWindowFlags(QtCore.Qt.WindowTitleHint | QtCore.Qt.WindowCloseButtonHint) – LozzerJP Nov 23 '21 at 14:14
4

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.

Michael Bishop
  • 4,240
  • 3
  • 37
  • 41
3

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);
  }
Predelnik
  • 5,066
  • 2
  • 24
  • 36
2

As the solution for PyQt4 from @amos didn't work for me and the version of PyQt4 is deprecated, here is my solution on how to remove the "?" in the dialog-box in PyQt5:

class window(QDialog):
    def __init__(self):
        super(window, self).__init__()
        loadUi("window.ui", self)
        self.setWindowFlag(QtCore.Qt.WindowContextHelpButtonHint,False)  # This removes it
0

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
Himanshu
  • 31,810
  • 31
  • 111
  • 133