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. My App Getting Focus from other Windows Apps
Forum Updated to NodeBB v4.3 + New Features

My App Getting Focus from other Windows Apps

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 1.8k 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.
  • R Offline
    R Offline
    Ray E
    wrote on last edited by
    #1

    Hi,

    After a delay, I would like my app to get focus from the other apps, running in Windows.
    For example: my app will start. The user may then do other tasks, such as MS Word. After a delay, my app, using QTimer, will trigger a new dialog window. I would like that dialog to have focus. (Yes, not very nice to the user.)

    Can this be done?
    Thanks

    JonBJ 1 Reply Last reply
    0
    • R Ray E

      Hi,

      After a delay, I would like my app to get focus from the other apps, running in Windows.
      For example: my app will start. The user may then do other tasks, such as MS Word. After a delay, my app, using QTimer, will trigger a new dialog window. I would like that dialog to have focus. (Yes, not very nice to the user.)

      Can this be done?
      Thanks

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Ray-E
      I didn't think you could do this these days under Windows (might depend which version, might be OK in XP). OS stops apps stealing focus from other apps, for good reason.

      Yes, not very nice to the user.

      I'm in the middle of typing into Word, and you want a dialog to pop up on a timer and take focus? What is the point, given that it will make your software unusably irritating?

      1 Reply Last reply
      1
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by
        #3

        This might work with

        void QWidget::activateWindow()
        

        and

        void QWindowsWindowFunctions::setWindowActivationBehavior(QWindowsWindowFunctions::WindowActivationBehavior behavior)
        

        But really, as a user, I hate that.

        JonBJ 1 Reply Last reply
        1
        • B Bonnie

          This might work with

          void QWidget::activateWindow()
          

          and

          void QWindowsWindowFunctions::setWindowActivationBehavior(QWindowsWindowFunctions::WindowActivationBehavior behavior)
          

          But really, as a user, I hate that.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Bonnie
          Given that https://doc.qt.io/qt-5/qwidget.html#activateWindow states as I suggested:

          On Windows, if you are calling this when the application is not currently the active one then it will not make it the active window. It will change the color of the taskbar entry to indicate that the window has changed in some way. This is because Microsoft does not allow an application to interrupt what the user is currently doing in another application.

          how will this achieve what the OP says he wants to happen?

          B 1 Reply Last reply
          1
          • JonBJ JonB

            @Bonnie
            Given that https://doc.qt.io/qt-5/qwidget.html#activateWindow states as I suggested:

            On Windows, if you are calling this when the application is not currently the active one then it will not make it the active window. It will change the color of the taskbar entry to indicate that the window has changed in some way. This is because Microsoft does not allow an application to interrupt what the user is currently doing in another application.

            how will this achieve what the OP says he wants to happen?

            B Offline
            B Offline
            Bonnie
            wrote on last edited by Bonnie
            #5

            @JonB So I provide another function that has

            QWindowsWindowFunctions::AlwaysActivateWindow 1 The window is always activated, even when the calling process is not the active process.

            But they'll need the OP to test them.
            Actually when I was using activateWindow several years ago, it does not act like the doc says. It does interrupt the user...

            JonBJ 1 Reply Last reply
            1
            • B Bonnie

              @JonB So I provide another function that has

              QWindowsWindowFunctions::AlwaysActivateWindow 1 The window is always activated, even when the calling process is not the active process.

              But they'll need the OP to test them.
              Actually when I was using activateWindow several years ago, it does not act like the doc says. It does interrupt the user...

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Bonnie
              Hmm, then worth a try! Maybe that was an old version of Windows? That's why I said "might depend which version, might be OK in XP", I thought that over the years Windows OS has tightened up on this such that newer editions deliberately forbid this, as per what the Qt docs currently say....

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Ray E
                wrote on last edited by
                #7

                Thanks for the info.

                For the record, I do not like a window getting focus. This is a very special case.
                I have a co-worker studying for her nursing boards. She, and her husband, say that she needs pushed. I have a program to ask her questions. The idea is to pop-up a question while she is working. If she tries not to answer, it will keep popping up.

                Thanks

                JonBJ 1 Reply Last reply
                0
                • R Ray E

                  Thanks for the info.

                  For the record, I do not like a window getting focus. This is a very special case.
                  I have a co-worker studying for her nursing boards. She, and her husband, say that she needs pushed. I have a program to ask her questions. The idea is to pop-up a question while she is working. If she tries not to answer, it will keep popping up.

                  Thanks

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Ray-E
                  It's not the "popping up" that is the issue, it's the stealing/gaining focus. Windows has plenty of ways for indicating pop up messages.

                  But I should be interested to hear from you: I am suggesting you cannot do this under current Windows even if you want to, are you saying it does work the way you described, e.g. interrupting user in another application?

                  B 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Ray-E
                    It's not the "popping up" that is the issue, it's the stealing/gaining focus. Windows has plenty of ways for indicating pop up messages.

                    But I should be interested to hear from you: I am suggesting you cannot do this under current Windows even if you want to, are you saying it does work the way you described, e.g. interrupting user in another application?

                    B Offline
                    B Offline
                    Bonnie
                    wrote on last edited by Bonnie
                    #9

                    @JonB I've tried with following code in my Win10

                    MainWindow::MainWindow(QWidget *parent)
                        : QMainWindow(parent)
                        , ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                        QWindowsWindowFunctions::setWindowActivationBehavior(QWindowsWindowFunctions::AlwaysActivateWindow);
                        auto timer = new QTimer(this);
                        connect(timer, &QTimer::timeout, [=](){
                            activateWindow();
                            if(isMinimized())
                                setWindowState(windowState() & ~Qt::WindowMinimized);
                        });
                        timer->start(10 * 1000);
                    }
                    

                    The MainWindow does "popup" every 10 seconds and can steal input focus from another app.
                    But it can't steal from Task Manager / apps run as admin, unless itself is also run as admin .
                    And it can't steal focus without setting AlwaysActivateWindow.
                    But if run from "start debugging" in Qt Creator, it can steal from any app, including the admin ones.

                    JonBJ 1 Reply Last reply
                    2
                    • B Bonnie

                      @JonB I've tried with following code in my Win10

                      MainWindow::MainWindow(QWidget *parent)
                          : QMainWindow(parent)
                          , ui(new Ui::MainWindow)
                      {
                          ui->setupUi(this);
                          QWindowsWindowFunctions::setWindowActivationBehavior(QWindowsWindowFunctions::AlwaysActivateWindow);
                          auto timer = new QTimer(this);
                          connect(timer, &QTimer::timeout, [=](){
                              activateWindow();
                              if(isMinimized())
                                  setWindowState(windowState() & ~Qt::WindowMinimized);
                          });
                          timer->start(10 * 1000);
                      }
                      

                      The MainWindow does "popup" every 10 seconds and can steal input focus from another app.
                      But it can't steal from Task Manager / apps run as admin, unless itself is also run as admin .
                      And it can't steal focus without setting AlwaysActivateWindow.
                      But if run from "start debugging" in Qt Creator, it can steal from any app, including the admin ones.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @Bonnie
                      Interesting, and surprising. Does seem then that https://doc.qt.io/qt-5/qwidget.html#activateWindow

                      On Windows, if you are calling this when the application is not currently the active one then it will not make it the active window. It will change the color of the taskbar entry to indicate that the window has changed in some way. This is because Microsoft does not allow an application to interrupt what the user is currently doing in another application.

                      is no longer the case!

                      1 Reply Last reply
                      1
                      • R Offline
                        R Offline
                        Ray E
                        wrote on last edited by
                        #11

                        Thanks for the help. The user is running Windows 7. (Office pc and the owner is very reluctant to move to more current OS.) The activateWindow works well in this OS.
                        I'll keep the other in mind if I need it for a newer OS.

                        The user is thrilled with the functionality.

                        1 Reply Last reply
                        1

                        • Login

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