Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Issue while detecting the Focus status of my Application

Issue while detecting the Focus status of my Application

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 356 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CLBSII
    wrote on last edited by
    #1

    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 !

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      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;
              });
      
      1 Reply Last reply
      3
      • C Offline
        C Offline
        CLBSII
        wrote on last edited by
        #3

        Indeed it seems to work properly, thank you very much !

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved