简体   繁体   English

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

[英]Qt: QMainWindow with a close, minimize and help button

If I have a class which inherits QMainWindow, and I want it to only have the buttons; 如果我有一个继承QMainWindow的类,我希望它只有按钮; 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); 如果我将此代码用于窗口标志: 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. 如果我排除“WindowMinimizeButtonHint”,则只有一个帮助和关闭按钮。

How can I, if possible, make so there is a close, help AND minimize button ONLY? 如果可能的话,我怎么能这样只有一个关闭,帮助和最小化按钮?

According to Microsoft's documentation .. 根据微软的文档 ..

WS_EX_CONTEXTHELP cannot be used with the WS_MAXIMIZEBOX or WS_MINIMIZEBOX styles. WS_EX_CONTEXTHELP不能与WS_MAXIMIZEBOX或WS_MINIMIZEBOX样式一起使用。

which are the underlying windows system flags for Qt::WindowContextHelpButtonHint , Qt::WindowMinimizeButtonHint and Qt::WindowMaximizeButtonHint . 这是Qt::WindowContextHelpButtonHintQt::WindowMinimizeButtonHintQt::WindowMaximizeButtonHint的基础Windows系统标志。

I don't think you can do this directly in Qt. 我认为你不能直接在Qt中这样做。 I played around with the "Window Flags" example that ships with Qt and cannot get any combination that works. 我玩了Qt附带的“Window Flags”示例,无法获得任何有效的组合。

If you really need this, you will probably have to use the Windows API directly. 如果您真的需要这个,您可能必须直接使用Windows API。 Here is a function I have used to enable/disable the close button in a Window. 这是我用来启用/禁用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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM