简体   繁体   中英

Remove WindowSystemMenuHint from QDialog in Qt

I want to remove system menu and context menu from QDialog title bar in Qt. I have written code below but it does not work.

Qt::WindowFlags flags = windowFlags();
Qt::WindowFlags helpFlag = Qt::WindowContextHelpButtonHint;
flags = flags & (~helpFlag);

Qt::WindowFlags systemMenuFlag = Qt::WindowSystemMenuHint;
flags = flags & (~systemMenuFlag);
setWindowFlags(flags);

when I print windowFlags I am getting following output which still contents WindowSystemMenuHint .

Qutout :: QFlags(0x1|0x2|0x1000|0x2000|0x8000000).

How can I remove 0x2000 ie WindowSystemMenuHint ?

From the Qt Docs

The CustomizeWindowHint flag is used to enable customization of the window controls. This flag must be set to allow the WindowTitleHint , WindowSystemMenuHint , WindowMinimizeButtonHint , WindowMaximizeButtonHint and WindowCloseButtonHint flags to be changed.

So try this:

Qt::WindowFlags flags = windowFlags();
flags |= Qt::CustomizeWindowHint;
flags &= ~Qt::WindowContextHelpButtonHint;
flags &= ~Qt::WindowSystemMenuHint;
setWindowFlags(flags);

Update for Qt 4.8

Browsing through the Qt source , you can see that the system menu hint is restored when either of Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowContextHelpButtonHint Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowContextHelpButtonHint Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint | Qt::WindowContextHelpButtonHint is set.

So this will do it:

Qt::WindowFlags flags = windowFlags();
flags |= Qt::CustomizeWindowHint;
flags &= ~Qt::WindowContextHelpButtonHint;
flags &= ~Qt::WindowSystemMenuHint;
flags &= ~Qt::WindowMinMaxButtonsHint;
flags &= ~Qt::WindowCloseButtonHint;
setWindowFlags(flags);

But you will not have a close button anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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