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. FocusWindowChange
Qt 6.11 is out! See what's new in the release blog

FocusWindowChange

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.1k Views 1 Watching
  • 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.
  • O Offline
    O Offline
    Oumayma
    wrote on last edited by
    #1

    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:

    Captura desde 2023-01-15 13-41-53.png

    When my app has not focus:

    Captura desde 2023-01-15 13-44-26.png

    M 1 Reply Last reply
    0
    • O Oumayma

      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:

      Captura desde 2023-01-15 13-41-53.png

      When my app has not focus:

      Captura desde 2023-01-15 13-44-26.png

      M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      Hi,

      Have a try with this signal instead:
      [signal] void QGuiApplication::applicationStateChanged(Qt::ApplicationState state)

      O 1 Reply Last reply
      0
      • M mpergand

        Hi,

        Have a try with this signal instead:
        [signal] void QGuiApplication::applicationStateChanged(Qt::ApplicationState state)

        O Offline
        O Offline
        Oumayma
        wrote on last edited by
        #3

        @mpergand Could you give me an implementation example?

        M 1 Reply Last reply
        0
        • O Oumayma

          @mpergand Could you give me an implementation example?

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4
          QObject::connect(&a, &QGuiApplication::applicationStateChanged, [&](Qt::ApplicationState state)
              {
              qDebug()<<state;
              if(state==Qt::ApplicationInactive)
                  {
                  }
             else
                  {
                  }
              }
          
          O 1 Reply Last reply
          0
          • M mpergand
            QObject::connect(&a, &QGuiApplication::applicationStateChanged, [&](Qt::ApplicationState state)
                {
                qDebug()<<state;
                if(state==Qt::ApplicationInactive)
                    {
                    }
               else
                    {
                    }
                }
            
            O Offline
            O Offline
            Oumayma
            wrote on last edited by
            #5

            @mpergand still not working

            Cobra91151C 1 Reply Last reply
            0
            • O Oumayma

              @mpergand still not working

              Cobra91151C Offline
              Cobra91151C Offline
              Cobra91151
              wrote on last edited by
              #6

              @Oumayma

              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:

              Window_Focus_Example.gif

              Happy coding!

              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