Qt disable clicking on the left corner icon
-
I am using win10 and qt5.
How could I disable display the pop up menu, when I click on the left corner icon? So I want everything to remain the same except disable the pop up.
I have tried
setWindowFlags()
with a loads of flags, but it does not helped for me. -
There's no way to do that in Qt alone, but, since it's Windows specific anyway, you can use the native api. Just override the native event handler in your window class and ignore specific Windows messages:
bool YourWindowClass::nativeEvent(const QByteArray& eventType, void* message, long* result) { MSG* msg = static_cast<MSG*>(message); // Disables menu when left clicked on icon if (msg->message == WM_SYSCOMMAND && (msg->wParam & 0xFFF0) == SC_MOUSEMENU) { *result = 0; return true; } // Disables menu when right clicked anywhere on the title bar if (msg->message == WM_NCRBUTTONDOWN || msg->message == WM_NCRBUTTONUP) { *result = 0; return true; } return QMainWindow::nativeEvent(eventType, message, result); }
You need to
#include <Windows.h>
for these WinAPI types and constants. -
@kocka
this is a Windows Window Manager feature and out of the scope of Qt.
I believe thats not possible afterall. -
w.setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint);
makes the following:
so I belive that there is a solution.If I also add
Qt::WindowMinimizeButtonHint
, the whole bar show becauseQt::WindowMinimizeButtonHint
,Qt::WindowCloseButtonHint
..... impliesQt::WindowSystemMenuHint
for it to work. -
@kocka
if thats ok for you.
i assumed you wanted to keep the icon. -
I want to keep the icon, and I want to keep the close up button as well, I just mentioned, this method is does not working.
-
There's no way to do that in Qt alone, but, since it's Windows specific anyway, you can use the native api. Just override the native event handler in your window class and ignore specific Windows messages:
bool YourWindowClass::nativeEvent(const QByteArray& eventType, void* message, long* result) { MSG* msg = static_cast<MSG*>(message); // Disables menu when left clicked on icon if (msg->message == WM_SYSCOMMAND && (msg->wParam & 0xFFF0) == SC_MOUSEMENU) { *result = 0; return true; } // Disables menu when right clicked anywhere on the title bar if (msg->message == WM_NCRBUTTONDOWN || msg->message == WM_NCRBUTTONUP) { *result = 0; return true; } return QMainWindow::nativeEvent(eventType, message, result); }
You need to
#include <Windows.h>
for these WinAPI types and constants. -
Thanks that works for me.
If I double click on the icon the window close itself, could you show me the solution? Thanks in advance.
-
@kocka It's similar to the
SC_MOUSEMENU
check, but the param in that case isSC_CLOSE
. The problem is this would also disable closing via clicking the X button, ALT+F4 or via context menu on the taskbar, so you'd need to figure out what caused this message. You could for example check the cursor position to see if it's above the corner where the icon is.Out of curiosity - why are trying to do all this? These features are a standard Windows behavior and disabling them is generally not a good idea. Your app may seem broken to people used to having those features on every other app.
-
Actually I am learing Qt, and I find this pop up and double click stuff, and I was curious how could I disable it.
This message disable double left click within nonclient area.
if(msg->message == WM_NCLBUTTONDBLCLK) { *result = 0; return true; }
-
@kocka Ok, so this is not very Qt related. Qt mostly deals with the client area of the window. The frame of the window and stuff on it are managed by the native platform specific window manager. Qt only interacts with it in limited scenarios that are more or less cross-platform, so like changing title or window type (dialog, popup, toolbox etc.). The more platform specific stuff, like those context menus, are not usually exposed through Qt interface directly, but you can dig into them through native APIs. This of course makes your code not cross-platform, so in a serious project stuff like this would be put in a platform specific
#ifdef
. -
Okey, I know that this is a windows stuff, and thanks for you help.