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. How to detect if a window has been put to background or minimized?

How to detect if a window has been put to background or minimized?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 6.5k 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.
  • L Offline
    L Offline
    lgos43
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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);
      }@

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgos43
        wrote on last edited by
        #3

        [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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            There's also the "hideEvent":http://qt-project.org/doc/qt-5/qwidget.html#hideEvent function that you may use

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            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