QTextEdit goes underneath android keyboard when editing
-
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 aQTextEdit.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_Hwidget.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.
-
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 aQTextEdit* 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...
-
Hi and welcome to devnet,
You should add:
- Which version of Qt you are using
- Which version of Android shows that issue
-
Hi and welcome to devnet,
You should add:
- Which version of Qt you are using
- Which version of Android shows that issue
@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
-
Additionally, I've noticed that this also gets triggered when using the backspace key...
-
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 aQTextEdit* 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...
-
B bepispasta has marked this topic as solved