PDA

View Full Version : Catch maximize and restore events.



QT8seven
24th December 2011, 20:41
I need to know when a window is restored or maximized. When this state-change happens, I need to take an action. I need to be careful however that this action only occurs once per state-change. I have the following code:


void TextArea::changeEvent(QEvent* event)
{
if (event->type() == QEvent::WindowStateChange)
{
if (static_cast<QWindowStateChangeEvent*>(event)->oldState() == windowState())
{
return;
}
if (isMaximized())
{

}
else
{

}
}
QWidget::changeEvent(event);
}

Even with the preceding code, the action is firing twice with the old-state being 0 and new state being 2. Is there any way to ensure that this fires only once?

QT8seven
27th December 2011, 02:32
Am I going to need to correct this by filtering out with timer-tick-counts?

Edit:
Currently I am thinking about doing a single-shot timer which calls a function that checks the window-state and takes action based on the window-state at that time. With this I could ensure that the action occurs only once as well as that the action taken matches the current window-state. This would cause a delay in the action, but seems to be the best option currently. Please let me know if I am doing something wrong which could be fixed to avoid this method.

Edit
While I have not found a better way to fix this, I want to add some information for any future people that find this thread:

It seems that the changeEvent is called twice for maximizing a window and once for restoring it down to it's original size. Perhaps someone can add more information as to why and perhaps how to segregate these triggers. For now, I am using a single-shot timer with '0' passed for time. Since this puts it at the end of the message-queue it causes no delay and will not be called twice.

grantbj74
10th January 2012, 07:34
This works for me:



void MyQtApp::changeEvent(QEvent *e)
{
if(e->type() == QEvent::WindowStateChange) {

if(isMinimized()) {

// MINIMIZED

} else {

// NORMAL/MAXIMIZED ETC

}
}

e->accept();
}

Spitfire
11th January 2012, 09:54
void MainWindow::changeEvent( QEvent* e )
{
if( e->type() == QEvent::WindowStateChange )
{
QWindowStateChangeEvent* event = static_cast< QWindowStateChangeEvent* >( e );

if( event->oldState() & Qt::WindowMinimized )
{
qDebug() << "Window restored (to normal or maximized state)!";
}
else if( event->oldState() == Qt::WindowNoState && this->windowState() == Qt::WindowMaximized )
{
qDebug() << "Window Maximized!";
}
}
}
Change event is emitted for many reasons so you can see it more than once during single WindowStateChange.
You should not worry about that anyway because changes you're interested in are happening only once.
From what you said I understand that you need to know when window is restored from minimized state (to normal or maximized) and when it's maximized from normal state.
You're not interested in knowing when window was minimized, right?
Code above does just that without need for timer.

bkudrle
25th January 2014, 01:56
Worked well, Spitfire. Thanks so much!