Qt:QMainWindow带有关闭,最小化和帮助按钮

时间:2012-01-27 19:39:30

标签: c++ windows qt

如果我有一个继承QMainWindow的类,我希望它只有按钮;在窗口栏中关闭,最小化和帮助,我该怎么办?

如果我将此代码用于窗口标志: setWindowFlags(Qt::Window | Qt::WindowContextHelpButtonHint | Qt::WindowMinimizeButtonHint); 它会生成一个窗口,其中包含最大化,最小化和关闭按钮。

如果我排除“WindowMinimizeButtonHint”,则只有一个帮助和关闭按钮。

如果可能的话,我怎么能这样只有一个关闭,帮助和最小化按钮?

2 个答案:

答案 0 :(得分:5)

根据微软的documentation ..

  

WS_EX_CONTEXTHELP不能与WS_MAXIMIZEBOX一起使用   WS_MINIMIZEBOX样式。

这是Qt::WindowContextHelpButtonHintQt::WindowMinimizeButtonHintQt::WindowMaximizeButtonHint的基础Windows系统标记。

答案 1 :(得分:0)

我认为你不能直接在Qt中这样做。我玩了Qt附带的“Window Flags”示例,无法获得任何有效的组合。

如果您真的需要这个,您可能必须直接使用Windows API。这是我用来启用/禁用Window中关闭按钮的函数。您可以根据自己的需要调整它。 (或者,保持简单,只需在表单的某处添加一个额外的“帮助”按钮!: - ))

#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));
}