How to detect if a window has been put to background or minimized?
-
Hi,
The title says it all. I want to do something evverytime my window is minimized, or put to background, everytime I switch to other window.
I thought it had to do with losing focus so I tried :@
bool Note::eventFilter(QObject *object, QEvent *event)
{
if( event->type() == QEvent::FocusOut )
{
Note::writeToFile();
return true;
}return false;
}
@with
@installEventFilter(this);
@
on the cosntructor.
But it didn't work. It works with QEvent::KeyPress however.
-
Welcome to the forum.
Try to catch the QEvent::WindowStateChange
e.g
@bool Widget::event(QEvent *evt){
if (evt->type() == QEvent::WindowStateChange) {
qDebug() << "WIndow changed =" << evt->type() << endl;
if(windowState().testFlag(Qt::WindowMaximized) == true) {
qDebug() << "Event Maximize " << endl;
return true;
}
return true;
}
return QWidget::event(evt);
}@ -
[quote author="Dheerendra" date="1406734757"]Welcome to the forum.
Try to catch the QEvent::WindowStateChange
e.g
@bool Widget::event(QEvent *evt){
if (evt->type() == QEvent::WindowStateChange) {
qDebug() << "WIndow changed =" << evt->type() << endl;
if(windowState().testFlag(Qt::WindowMaximized) == true) {
qDebug() << "Event Maximize " << endl;
return true;
}
return true;
}
return QWidget::event(evt);
}@[/quote]I forgot to mention I tried something similar but it doesnt cover the case when hte window is only put to background.
-
You can try to listen for "applicationStateChanged":http://qt-project.org/doc/qt-5/qguiapplication.html#applicationStateChanged signal in addition to Dheerendras suggestion.
But on some window managers you may find it hard to detect when your app is hidden.
-
Hi,
There's also the "hideEvent":http://qt-project.org/doc/qt-5/qwidget.html#hideEvent function that you may use