Qt dialog with no window icon (system menu)
Asked Answered
H

2

7

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.

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.

Hoop answered 5/8, 2009 at 21:18 Comment(3)
Which operating system? It will surely be platform-specific (if possible at all).Bradwell
Oh, sorry. For Windows, but hopefully a cross-platform solution exists.Hoop
forum.qt.io/topic/83618/remove-qapplication-window-icon/6Bragg
R
13

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.


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 with a Close button but without the system menu and window icon.

In that case, you will need some Windows specific code, similar to this (untested):

#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.

Remake answered 6/8, 2009 at 5:34 Comment(3)
Thanks for this idea. Unfortunately, your Windows code snippet works like the Qt case -- once the system menu disappears, so does the caption buttons. MSDN states that WS_MAXIMIZEBOX and WS_MINIMIZEBOX require WS_SYSMENU to be raised.Hoop
Yes, they do. You can get away with having a close button on the caption, however. I'll clear that up.Remake
I just want the close button (no system menu, no maximize/minimize), but this doesn't work for me. The close button disappears also. I have set the Qt window flags to Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint.Evangelicalism
U
0

Inspired by the answer from @richardwb , I did the following (Qt 5.12), to not affect the existing window flags for my QDialog:

setWindowFlag(Qt::CustomizeWindowHint, true);
setWindowFlag(Qt::WindowTitleHint, true);
setWindowFlag(Qt::WindowSystemMenuHint, false);
Unterwalden answered 30/9, 2021 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.