Issue while detecting the Focus status of my Application
-
Hi everyone,
I'm looking for a way to detect when my application is put in the background (for example after the user uses alt-tab).
After some researchs, I figure out that the best way to do it is to use the focus functionality since my application in based on a QMainWindow class.
So I set up the FocusPolicy as follow in my class definition function:
MainWindow::setFocusPolicy(Qt::StrongFocus);
When the program is running, I have a testing sequence that regularly check if my application has focus with
qDebug() << MainWindow::hasFocus();
And whatever I do (casually use my application, do atl-tab and use another ...). The focus return me false every time.
I assume that I'm missing some point, but I can't find what. Any suggestions ?
Thank you !
-
Hi,
There's a signal in QApplication that should meet your needs:
int main(int argc, char *argv[]) { QApplication app(argc, argv); QObject::connect(&app,&QApplication::applicationStateChanged,[](Qt::ApplicationState state) { qDebug()<<state; }); return app.exec(); }
In your MainWindow's constructor, you can do:
connect(qApp,&QApplication::applicationStateChanged,[] (Qt::ApplicationState state) { qDebug()<<state; });