windows 没有窗口图标的 Qt 对话框(系统菜单)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1235812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Qt dialog with no window icon (system menu)
提问by swongu
Is there a way to create a window (such as a QDialog
), without a window icon on the top-left corner? I have tried using a transparent icon but it leaves a blank space there.
有没有办法创建一个窗口(例如QDialog
),而左上角没有窗口图标?我曾尝试使用透明图标,但它在那里留下了空白。
Edit:richardwb's solution below removes the system menu, but also removes Minimize/Maximize/Close (caption buttons) as well. This might do for now, but hopefully there is a solution that preserves the captions buttons.
编辑:richardwb 下面的解决方案删除了系统菜单,但也删除了最小化/最大化/关闭(标题按钮)。现在可能会这样做,但希望有一种解决方案可以保留字幕按钮。
回答by richardwb
If you don't need any caption buttons at all, you can achieve this by setting some window flag hints:
如果您根本不需要任何标题按钮,您可以通过设置一些窗口标志提示来实现:
setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
Qt's Demo application has a sample application that lets you experiment with these flags (Qt Demo->Widgets->Window Flags) if you want to see what different combinations do.
Qt 的演示应用程序有一个示例应用程序,如果您想查看不同组合的作用,您可以使用该示例应用程序试验这些标志(Qt Demo->Widgets->Window Flags)。
On the other hand, if you want any of the Minimize/Maximize/Close buttons, you will notice Qt forces the system menu and window icon to show up. I think this is Qt generalizing the platforms a bit, as it's very easy to find examples of native Windows dialogs witha Close button but withoutthe system menu and window icon.
另一方面,如果您想要任何最小化/最大化/关闭按钮,您会注意到 Qt 强制显示系统菜单和窗口图标。我认为这是 Qt 对平台的概括,因为很容易找到带有关闭按钮但没有系统菜单和窗口图标的本地 Windows 对话框的示例。
In that case, you will need some Windows specific code, similar to this (untested):
在这种情况下,您将需要一些 Windows 特定的代码,与此类似(未经测试):
#if defined(Q_WS_WIN)
// don't forget to #include <windows.h>
HWND hwnd = winId();
LONG_PTR style = GetWindowLongPtr(hwnd, GWL_STYLE);
style &= ~WS_SYSMENU; // unset the system menu flag
SetWindowLongPtr(hwnd, GWL_STYLE, style);
// force Windows to refresh some cached window styles
SetWindowPos(hwnd, 0, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
#endif
Edit: As commented by swongu, this only works if you want to have a close button without a system menu. If you want a minimize/maximize button but no system menu, you're out of luck.
编辑:正如 swongu 所评论的,这仅在您想要一个没有系统菜单的关闭按钮时才有效。如果你想要一个最小化/最大化按钮但没有系统菜单,那你就不走运了。