FocusWindowChange
-
Hi, I am using the FocusWindowChange signal to detect when my application no longer has focus. I would like that when my application does not have focus, the timer that I use stops its execution and when it has focus again, the timer will run again. This works for me in some cases, but when I bring up my Qmenu and go to another application, my application keeps receiving mouse click signals and resumes running the timer.
QObject::connect(&a, &QGuiApplication::focusWindowChanged, [&](QWindow* window) { if(a.applicationState() == Qt::ApplicationInactive && w->activo){ qDebug()<<a.applicationState(); w->timer.stop(); }else if(a.applicationState() == Qt::ApplicationActive && !w->timer.isActive() && w->activo){ qDebug()<<a.applicationState(); if(w->menudesplegado){ w->menu->show(); qDebug()<<"Esto con popup activo"; } w->timer.start(); } qDebug()<<window; });
When my app has focus:
When my app has not focus:
-
Hi, I am using the FocusWindowChange signal to detect when my application no longer has focus. I would like that when my application does not have focus, the timer that I use stops its execution and when it has focus again, the timer will run again. This works for me in some cases, but when I bring up my Qmenu and go to another application, my application keeps receiving mouse click signals and resumes running the timer.
QObject::connect(&a, &QGuiApplication::focusWindowChanged, [&](QWindow* window) { if(a.applicationState() == Qt::ApplicationInactive && w->activo){ qDebug()<<a.applicationState(); w->timer.stop(); }else if(a.applicationState() == Qt::ApplicationActive && !w->timer.isActive() && w->activo){ qDebug()<<a.applicationState(); if(w->menudesplegado){ w->menu->show(); qDebug()<<"Esto con popup activo"; } w->timer.start(); } qDebug()<<window; });
When my app has focus:
When my app has not focus:
-
Hi,
Have a try with this signal instead:
[signal] void QGuiApplication::applicationStateChanged(Qt::ApplicationState state) -
QObject::connect(&a, &QGuiApplication::applicationStateChanged, [&](Qt::ApplicationState state) { qDebug()<<state; if(state==Qt::ApplicationInactive) { } else { } }
-
Hello!
You can use the
event
method (https://doc.qt.io/qt-6/eventsandfilters.html). The list of event enums are available here: https://doc.qt.io/qt-6/qevent.html
Please, check out the text example below:Code:
.h (your header file)#include <QEvent> #include <QDebug> protected: bool event(QEvent *event) override;
.cpp
bool TestWidget::event(QEvent *event) { switch (event->type()) { case QEvent::WindowActivate: qDebug() << "Window has focus!!!"; break; case QEvent::WindowDeactivate: qDebug() << "Window lost focus!!!"; break; default: break; } return QWidget::event(event); }
Screenshot:
Happy coding!