Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

My App Getting Focus from other Windows Apps



  • 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



  • @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?



  • This might work with

    void QWidget::activateWindow()
    

    and

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

    But really, as a user, I hate that.



  • @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?



  • @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...



  • @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....



  • 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



  • @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?



  • @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.



  • @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!



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


Log in to reply