1

I want to remove the "?"-button from a Qt dialog like explained here, but somehow it doesn't work.

This is my code:

Qt::WindowFlags flags;
flags = (Qt::Dialog | Qt::WindowStaysOnTopHint) & 
          ~Qt::WindowContextHelpButtonHint;
setWindowFlags( flags );

I think that there is something wrong with the bitwise operators, but I don't know what..

Community
  • 1
  • 1
Zulakis
  • 7,859
  • 10
  • 42
  • 67
  • 1
    Possible duplicate of [How can I hide/delete the "?" help button on the "title bar" of a Qt Dialog?](http://stackoverflow.com/questions/81627/how-can-i-hide-delete-the-help-button-on-the-title-bar-of-a-qt-dialog) This actually is a duplicate. The OP didn't follow the instructions in the linked post correctly; the answer there is still the solution. – Jason C Dec 08 '15 at 05:35

1 Answers1

4

This works fine for me:

QDialog *dialog = new QDialog;
Qt::WindowFlags flags(Qt::WindowTitleHint);
dialog->setWindowFlags(flags);

The most common way, however, is to pass the flags in the constructor:

QDialog *dialog = new QDialog(0, Qt::WindowTitleHint);

EDIT: I think there is a misunderstanding about the QFlags operators (see comments below). This example might clear it up:

Qt::WindowFlags flags(Qt::Dialog | Qt::WindowStaysOnTopHint);
qDebug() << flags.testFlag(Qt::WindowContextHelpButtonHint); // false because the flag hasn't been added
flags = flags | Qt::WindowContextHelpButtonHint;
qDebug() << flags.testFlag(Qt::WindowContextHelpButtonHint); // true because it has been added
flags = flags & ~Qt::WindowContextHelpButtonHint;
qDebug() << flags.testFlag(Qt::WindowContextHelpButtonHint); // false because it has been removed

The penultimate line of code removes Qt::WindowContextHelpButtonHint from flags. It does not add a "negative" flag.

At least that is my understanding.

Anthony
  • 8,570
  • 3
  • 38
  • 46
  • This does work, even though I don't really understand why. On http://doc.qt.nokia.com/4.7/qt.html#WindowType-enum it says `Gives the window a title bar.` for `WindowTitleHint` – Zulakis Nov 04 '12 at 21:28
  • @Zulakis It's because there are flags enabled by default. When you call setFlags, you replace all of the currently enabled flags (in your case, the default ones) with the ones you put in the argument. So when you call `setFlags(Qt::WindowTitleHint)` all of the other flags except WindowTitleHint--including the one that displays the question mark--get cleared. – Anthony Nov 04 '12 at 22:10
  • I'm not familiar with the syntax you used, so I have no idea whether or not it should work. It looks like you were trying to say you wanted X and Y, but not Z. If that's the case, you don't need to say the last part. – Anthony Nov 04 '12 at 22:19
  • Yes that's correct. I wanted `Qt::Dialog` and `Qt::WindowStaysOnTopHint` but not `Qt::WindowContextHelpButtonHint`. However, no matter if the `Qt::WindowContextHelpButtonHint` part is there or not the "?"-button is always there. – Zulakis Nov 04 '12 at 22:33
  • I think that's because the `Qt::Dialog` hint shows the question mark. – Anthony Nov 04 '12 at 22:57
  • Doesn't look like, my new flags are `Qt::Dialog | Qt::WindowStaysOnTopHint | Qt::WindowTitleHint`.. And no question mark is shown. However, IF `Qt::Dialog` would show it, the `& ~Qt::WindowContextHelpButtonHint` should've removed it... – Zulakis Nov 04 '12 at 23:05
  • The QFlags class is just a template class and isn't aware of the meanings of the flags. So all the flags object knows is "Dialog", "Window Stays On Top", and "Window Title". When you negate `Qt::WindowContextHelpButtonHint`, that doesn't actually do anything, because it isn't one of the stored flags. So the dialog object only sees the flags "QDialog", "Window Stays On Top", and "Window Title Hint". It doesn't get "Don't Show the Help Button". And when it sees the QDialog flag, it adds the help button. – Anthony Nov 04 '12 at 23:44
  • No, they actually are enums which can be manipulated using bitwise operators. Have a look here: http://doc.qt.nokia.com/4.7/qt.html#WindowType-enum – Zulakis Nov 04 '12 at 23:45
  • Yes, I'm aware of the Qt enums. Well, see the edit and maybe that will clear it up. Otherwise I got nothing. :) – Anthony Nov 05 '12 at 00:12
  • Ah, now I understand what you mean. Thanks for leading me to the right direction ;-) Maybe have a guess at http://stackoverflow.com/questions/13223462/qt-set-keymap-for-qwsserver? – Zulakis Nov 05 '12 at 00:16