how to ignore the event when I click on the button to maximize the window (windowMaximizeButtonHint())
-
Hi everyone I would like to implement codes so that the event is ignored when I click on the button to maximize the window "Qt::WindowMaximizeButtonHint()" without using "~Qt::WindowMaximizeButtonHint()" i need to ignore the event at first, which means the event is ignored if I click the first two windows when the application starts. How can I do? give me advice. thank you so much!
-
I tried with:
MainWindow.h
virtual void changeEvent(QEvent* event);
MainWindow.cpp
void MainWindow::changeEvent(QEvent *event)
{
if(event->type() == QEvent::WindowStateChange)
{
hide();
event->ignore();
}
return QMainWindow::changeEvent(event);
}I know that the button event is not square Qt::WindowMaximizeButtonHint (). but with codes as indicated above if I click on that button the window disappears or rather the application disappears. why? what's wrong?
-
@Domenico
First: I think when youhide()
the main window, and it's the only window, that causes application exit. Why did you put that in anway, you just want to try toignore()
the event?Second: I happened to be looking at something similar last week. See https://forum.qt.io/topic/104184/is-it-possible-to-intercept-maximizing-the-main-window. In my case I wanted to intercept the maximize window button to not do maximization and instead something of my own. The conclusion I came to was: this event only seems to be signalled after the native windowing system has done what is supposed to be done when the maximize button is clicked. You can react to it yourself in the slot, but you cannot stop the window maximizing. That was my conclusion. I left it open because I still didn't hear some definitive statement as to whether this is correct by one of the experts here.
-
Hello! First of all, you can set for example:
setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint);
, so your dialog no longer havemaximize
button. But If you really want to intercept the maximize event here is my code:bool Test::event(QEvent *event) { if (event->type() == QEvent::WindowStateChange) { if (this->isMaximized()) { this->showNormal(); } } return QWidget::event(event); }
It's a bit tricky, but it handles such task. Also, there is option using
Win API
interceptingSW_SIZE, SW_SIZING
messages usingQt
bool QWidget::winEvent(MSG * message, long * result): https://doc.qt.io/archives/qt-4.8/qwidget.html#winEventHappy coding!
-
@Cobra91151
In my case (as per https://forum.qt.io/topic/104184/is-it-possible-to-intercept-maximizing-the-main-window), as I said there the code you show results in the window first being maximized by the OS window system, which the user sees, followed by it being restored to normal size. That is actually the kind of thing I ended up having to use, but it is not what I would call "intercepting" or "ignoring" the maximize button. -
Try to use
winEvent
it will help to actually intercept anyOS
message including maximize.Win API
docs: https://docs.microsoft.com/en-us/windows/desktop/winmsg/wm-size
https://docs.microsoft.com/en-us/windows/desktop/winmsg/wm-sizing -
@Cobra91151
How will that help me given that I use Qt for platform independence? I support Windows & Linux, like many people.... The OP has never mentioned Windows-only. -
It is not possible to make it working using
Qt
for platform independence. Because it doesn't have such feature built in. The only easy way is to use platform specific code and add checks for differentOS
, such asWindows
andLinux
. You can also try to write portable lib/code by yourself omittingQt
.By the way,
Qt
framework works the same, it has platform specific code and checks which one to use behind the scene :)Also, I would like to mention new
Qt
method:bool QWidget::nativeEvent(const QByteArray &eventType, void *message, long *result)
-
So here is with
nativeEvent
and platform checks forWindows
andLinux
.bool Test::nativeEvent(const QByteArray &eventType, void *message, long *) { qDebug() << eventType; MSG *msg = static_cast<MSG *>(message); #ifdef __linux__ //Linux code here #elif _WIN32 if (msg->message == WM_SYSCOMMAND) { if (msg->wParam == SC_MAXIMIZE) { QMessageBox::information(this, "Test", "Maximizing...", QMessageBox::Ok); //Add your code here return true; } } #endif return false; }
Screenshot:
-
@JonB @Cobra91151
thanks a lot to everyone!! worked with the method:bool QWidget::nativeEvent(const QByteArray &eventType, void *message, long *result)
With this method it does not allow to enlarge the window when I click on maximizes! is the code I meant. also the code:
bool Test::event(QEvent *event)
{
if (event->type() == QEvent::WindowStateChange) {
if (this->isMaximized()) {
this->showNormal();
}
}return QWidget::event(event);
}
They are fine but it is not ignored if you click the button before it enlarges and immediately afterwards it returns to normal, it is not what I meant. Thanks again!
-
@Domenico said in how to ignore the event when I click on the button to maximize the window (windowMaximizeButtonHint()):
bool Test::event(QEvent *event)
{
if (event->type() == QEvent::WindowStateChange) {
if (this->isMaximized()) {
this->showNormal();
}
}
return QWidget::event(event);}
They are fine but it is not ignored if you click the button before it enlarges and immediately afterwards it returns to normal, it is not what I meant.This is the normal behavior for this code. The
QEvent
can not prevent the OS from maximizing the window. So your window will maximize first, before the event sets it back to normal size.