views:

1520

answers:

1

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.

+1  A: 

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.

richardwb
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.
swongu
Yes, they do. You can get away with having a close button on the caption, however. I'll clear that up.
richardwb