Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QTextEdit goes underneath android keyboard when editing

QTextEdit goes underneath android keyboard when editing

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 2 Posters 268 Views 2 Watching
  • 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.
  • B Offline
    B Offline
    bepispasta
    wrote last edited by
    #1

    I'm trying to make a simplistic text editor (or notes-app clone), for desktop and android. As a minimum-reproducible-example, I have a central widget, with a QVBoxLayout, that contains a QTextEdit.

    On mobile (I've only tested my android phone), the QTextEdit initially resizes to the remaining available space when the android keyboard is visible, bringing in a scrollbar if necessary for longer texts - which is intended behavior.

    But when editing, if I insert a new line or move the cursor up or down by a line, the QTextEdit "grows" back to its fullscreen size, and renders half of its text and UI under the keyboard.

    As another measure, I tried adding a second widget to the QVBoxLayout. Sure enough, it is visible above the keyboard initially when text is being entered, but then disappears under the keyboard when I press enter or move the cursor.

    Sometimes, I can bring it back to its correct position/layout/size if I close and reopen the keyboard, but that isn't consistent either.

    How do I make the QTextEdit (and any widget in general) not resize or grow when the android on-screen keyboard is still active?

    Minimal example:

    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include <QTextEdit>
    #include <QLayout>
    
    class Widget : public QWidget {
        Q_OBJECT
        QTextEdit* child;
        QLayout* layout;
    
    public:
        Widget(QWidget *parent = nullptr);
        ~Widget();
    };
    #endif // WIDGET_H
    
    

    widget.cpp

    #include "widget.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        child = new QTextEdit();
        layout = new QVBoxLayout();
        layout->addWidget(child);
        setLayout(layout);
    }
    
    Widget::~Widget() {
        delete layout;
        delete child;
    }
    

    main.cpp

    #include "widget.h"
    
    #include <QApplication>
    
    int main(int argc, char *argv[]) {
        QApplication a(argc, argv);
        Widget w;
        w.show();
        return a.exec();
    }
    

    To trigger: Build for android, and then type in 30 lines or so (can be blank), and try to navigate to the bottom or last line - then try moving the cursor one line above, and then back down - the last line (and its text) disappears under the keyboard.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bepispasta
      wrote last edited by
      #5

      I've found a workaround - since the QTextEdit initially is the correct size when I open the keyboard, I can call setMaximumHeight(this->size().height()), which prevents it from growing vertically on these other events. In other words, for a QTextEdit* editor, you would:

      QTextEdit* editor = /* anything here, or if you're using this inside a QTextEdit subclass, use "this" instead... */;
      
      QApplication* app = (QApplication*) (QApplication::instance());
      QObject::connect(app->inputMethod(), &QInputMethod::keyboardRectangleChanged, [&]() {
              if (app->inputMethod()->keyboardRectangle().height() == 0)
                      // keyboard is closed
                      editor->setMaximumHeight(QWIDGETSIZE_MAX);
              else
                      // keyboard is open, limit maximum vertical height
                      editor->setMaximumHeight(editor->height());
      });
      

      This happens to work for my use case, but might fail others...

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

        Hi and welcome to devnet,

        You should add:

        • Which version of Qt you are using
        • Which version of Android shows that issue

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

        B 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          You should add:

          • Which version of Qt you are using
          • Which version of Android shows that issue
          B Offline
          B Offline
          bepispasta
          wrote last edited by
          #3

          @SGaist Right!

          I'm using Qt 6.10.1 for android arm64-v8a (Android API lv 36), and I've tested on my phone, android 16 (ASP Nov. 1) with OneUI 8.0

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bepispasta
            wrote last edited by
            #4

            Additionally, I've noticed that this also gets triggered when using the backspace key...

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bepispasta
              wrote last edited by
              #5

              I've found a workaround - since the QTextEdit initially is the correct size when I open the keyboard, I can call setMaximumHeight(this->size().height()), which prevents it from growing vertically on these other events. In other words, for a QTextEdit* editor, you would:

              QTextEdit* editor = /* anything here, or if you're using this inside a QTextEdit subclass, use "this" instead... */;
              
              QApplication* app = (QApplication*) (QApplication::instance());
              QObject::connect(app->inputMethod(), &QInputMethod::keyboardRectangleChanged, [&]() {
                      if (app->inputMethod()->keyboardRectangle().height() == 0)
                              // keyboard is closed
                              editor->setMaximumHeight(QWIDGETSIZE_MAX);
                      else
                              // keyboard is open, limit maximum vertical height
                              editor->setMaximumHeight(editor->height());
              });
              

              This happens to work for my use case, but might fail others...

              1 Reply Last reply
              1
              • B bepispasta has marked this topic as solved

              • Login

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