3
votes

If I have a class which inherits QMainWindow, and I want it to only have the buttons; close, minimize and help in the window bar, how should I proceed?

If I use this code for the window flags: setWindowFlags(Qt::Window | Qt::WindowContextHelpButtonHint | Qt::WindowMinimizeButtonHint); It results in a window with maximize, minimize and close button.

If I exclude "WindowMinimizeButtonHint", there is only a help and close button.

How can I, if possible, make so there is a close, help AND minimize button ONLY?

2
It looks like a known bug: bugreports.qt-project.org/browse/QTBUG-8049JCooper

2 Answers

5
votes

According to Microsoft's documentation..

WS_EX_CONTEXTHELP cannot be used with the WS_MAXIMIZEBOX or WS_MINIMIZEBOX styles.

which are the underlying windows system flags for Qt::WindowContextHelpButtonHint, Qt::WindowMinimizeButtonHint and Qt::WindowMaximizeButtonHint.

0
votes

I don't think you can do this directly in Qt. I played around with the "Window Flags" example that ships with Qt and cannot get any combination that works.

If you really need this, you will probably have to use the Windows API directly. Here is a function I have used to enable/disable the close button in a Window. You could probably adapt it for your purposes. (Or, keep it simple and just add an extra "help" button somewhere on your form! :-))

#include "Windows.h"
#include "WinUser.h"
typedef HMENU (WINAPI*pGetSystemMenu)(HWND, BOOL);
typedef BOOL (WINAPI*pEnableMenuItem)(HMENU, UINT, UINT);

void myapp::SetCloseButtonEnabled(QWidget *target, bool enabled) {
  // See msdn.microsoft.com/en-us/library/windows/desktop/ms647636(v=vs.85).aspx
  QLibrary user32(QLatin1String("user32"));
  pGetSystemMenu GetSystemMenu =
      (pGetSystemMenu)user32.resolve("GetSystemMenu");
  pEnableMenuItem EnableMenuItem =
      (pEnableMenuItem)user32.resolve("EnableMenuItem");
  HMENU menu = GetSystemMenu(target->winId(), false);
  EnableMenuItem(menu,
                 SC_CLOSE,
                 MF_BYCOMMAND | (enabled ? MF_ENABLED : MF_GRAYED));
}