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

Dialog apps with Qt::Popup attribute do not get focus when launched via shortcuts



  • Here is the minimal example that reproduces weird focus behavior, at least in Ubuntu Linux:

    #include <QtWidgets>
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QDialog *dialog = new QDialog();
        QHBoxLayout *layout = new QHBoxLayout(dialog);
        QLineEdit *lineedit = new QLineEdit(dialog);
        layout->addWidget(lineedit);
        dialog->setLayout(layout);
        dialog->setWindowFlags(Qt::Popup);
        dialog->setAttribute(Qt::WA_QuitOnClose);
        dialog->show();
        lineedit->setFocus();
        // dialog->raise();
        // dialog->activateWindow();
        return app.exec();
    }
    

    Running the app from terminal gives no surprises. But if I make an OS shortcut that launches the same app, more often than not I end up with the dialog sticking on top of all windows and with blinking cursor waiting there for user's input, but ignoring the input entirely. Instead, the input goes to another app that was previously in focus.

    I use Qt version 5.12. The same happens if I rewrite the code in python using PyQt5. It also does not depend on DE/window manager - I tried this in i3wm, Gnome and xfce - the situation is the same.

    I googled on this, and discovered hints to use raise() and windowActivate(), but found that they make no difference.

    So how do I make a standalone popup menu in Qt that might be called via shortcut without losing focus? I need the popup attribute, since my app needs to behave as a top-level menu (e.g., like dmenu or rofi), i.e. it should appear even above full-screen windows, it should not be tiled in tiling window managers, etc.



  • Can anybody at least reproduce the same issue? I've experiened it under X11 on four different hosts (Ubuntu + i3, Ubuntu + xfce4, Ubuntu + Gnome, Arch + i3), so it bothers me the issue is largely unknown. It makes an application consisting of a standalone popup dialog basically unusable under X11. Under Wayland there is no such problem. Apparently, in X11 something happens between the shortcut invocation and actual appearance of the widget that jumbles up the input behavior.


Log in to reply