Important: Please read the Qt Code of Conduct -

Move QWidget offscreen on linux?

  • Hi everyone.
    In some cases, we need to move QWidget outside the screen coordinate (move it with negative pos), but this behavior is not working under X11 platform(tested under ubuntu 18.04).

    Is it related to window manager or Qt?
    What is the solution for this?



  • Lifetime Qt Champion


    Can you explain what cases these are ?

  • Hi @SGaist
    Consider the situation when we use qtvirtualkeyboard in widget-based applications. when the virtual keyboard or onscreen keyboard is shown(click or touch on input widget) user expect to see what he/she is typing inside the input widget(see the input widget itself like Android Swiftkey) so it comes to mind that the input widget container(parent widget) should move off the screen whenever needed(input widget.rect().bottom() is greater than keyboard.rect().top())
    The expected behavior:
    alt text

    And here is my implementation:

    class LineEdit :public QLineEdit {
        LineEdit(QWidget *parent = nullptr);
        LineEdit(const QString&, QWidget *parent = nullptr);
        bool event(QEvent*) override;
        bool _moved = false;
        int _lastDiff = 0;
    LineEdit::LineEdit(QWidget *parent) :QLineEdit(parent) {
        setAttribute(Qt::WA_InputMethodEnabled, true);
        setInputMethodHints(inputMethodHints() | Qt::InputMethodHint::ImhDigitsOnly);
    LineEdit::LineEdit(const QString& txt, QWidget *parent) : QLineEdit(txt, parent) {
        setAttribute(Qt::WA_InputMethodEnabled, true);
        setInputMethodHints(inputMethodHints() | Qt::InputMethodHint::ImhDigitsOnly);
    bool LineEdit::event(QEvent* e) {
        const auto keyboard_rect = QGuiApplication::inputMethod()->keyboardRectangle();
        const auto keyboard_visible = QGuiApplication::inputMethod()->isVisible();
        const auto global_y = QWidget::mapToGlobal(rect().topLeft()).y() + height();
        const auto k_global_y = keyboard_rect.topLeft().y();
        const auto diff = k_global_y - global_y;
        const auto need_to_move = diff < 0;
        /* move main widget */
        if (keyboard_visible && !_moved && need_to_move) {
            _moved = true;
            _lastDiff = diff;
            const auto g = parentWidget()->frameGeometry();
            parentWidget()->move(g.x(), g.y() - qAbs(_lastDiff));
        /* roll back */
        if (!keyboard_visible && _moved) {
            _moved = false;
            const auto g = parentWidget()->frameGeometry();
            parentWidget()->move(g.x(), g.y() + qAbs(_lastDiff));
        return QLineEdit::event(e);

    Hope to clarify it.

  • I don't believe X11 will allow negative coordinates, as traditional geometry uses the form

    AxB+C+D where the (+) can also be a (-) to cause the offset to be from the right hand side or bottom of the display.

    You should however be able to use coordinates that are GREATER than the visible desktop.