Fix QTextEdit Overlap with Android Keyboard in Qt Android
Fix QTextEdit overlap with Android keyboard in Qt: use android:windowSoftInputMode=adjustPan and QInputMethod::keyboardRectangle to keep widgets visible.
QTextEdit overlaps Android on-screen keyboard during editing in Qt app
I’m developing a simple text editor (notes app clone) for desktop and Android using Qt. The central widget uses a QVBoxLayout containing a QTextEdit.
On Android, when the on-screen keyboard appears:
QTextEditinitially resizes correctly to the available space, adding a scrollbar if needed (desired behavior).- However, during editing (e.g., inserting a new line or moving the cursor up/down), the
QTextEditexpands to full screen size, causing text and UI to appear under the keyboard.
Adding other widgets to the QVBoxLayout shows the same issue: they are visible above the keyboard initially but disappear under it after editing actions.
Closing/reopening the keyboard sometimes restores the layout, but inconsistently.
How can I prevent QTextEdit (and other widgets) from resizing or expanding under the active Android on-screen keyboard?
Minimal Reproducible 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();
}
Keywords for search: Qt Android virtual keyboard resize, QTextEdit keyboard overlap, Qt mobile input handling, prevent widget resize on keyboard show.
To stop your QTextEdit from expanding under the Android on-screen keyboard during editing in Qt Android apps, tweak the android:windowSoftInputMode to “adjustPan” in your AndroidManifest.xml—this pans the window instead of resizing it wildly. If you need finer control, hook into QGuiApplication::inputMethod() to track the keyboard rectangle and manually resize your QVBoxLayout widgets. This combo fixes the inconsistent behavior where initial keyboard show works fine but cursor moves or new lines trigger full-screen overlap.
Contents
- Understanding the Qt Android Keyboard Overlap Issue
- Fix 1: AndroidManifest.xml Configuration
- Fix 2: Dynamic Resizing with QInputMethod
- Handling Layouts with Multiple Widgets
- Advanced: Custom Keyboard Detection via JNI
- Testing and Best Practices
- Sources
- Conclusion
Understanding the Qt Android Keyboard Overlap Issue
Ever built a simple notes app in Qt only to watch it crumble on Android? Your setup—a central QWidget with QVBoxLayout holding a QTextEdit—behaves perfectly on desktop. But fire it up on Android, tap to edit, and bam: the keyboard pops, QTextEdit shrinks nicely at first (scrollbar appears, good), then mid-typing or cursor-jumping, it balloons to full height. Text vanishes under the keyboard. Other widgets in the layout? Same disappearing act.
Why? Android’s soft input system fights Qt’s layout manager. By default, it might use “adjustResize,” which crams the keyboard by resizing your window. Editing events (like text insertion) trigger re-layouts, ignoring the keyboard space. Qt Widgets apps exacerbate this since they don’t auto-handle input like QML does. Searches for “qt android virtual keyboard resize” spike because devs hit this wall constantly.
Quick test: Add a QLabel above your QTextEdit. Keyboard shows? Label vanishes on edit. Closing/reopening keyboard flickers it back—classic symptom of unstable resize mode.
Fix 1: AndroidManifest.xml Configuration
Start here. It’s the lowest-effort win for qt android keyboard woes.
In Qt Creator (grab it via “qt creator android” if needed), your AndroidManifest.xml lives in android/res/values/ or gets generated on build. Edit the <activity> tag:
<activity android:name="org.qtproject.qt5.android.bindings.QtActivity"
android:windowSoftInputMode="adjustPan"
... >
- adjustPan: Shifts the whole window up so the focused field stays visible. No resize madness. Perfect for your QTextEdit.
- adjustResize: Android resizes the window (your initial behavior), but editing triggers over-expansion. Avoid unless tuned.
- adjustNothing: Keyboard overlays without moving anything—risky for bottom fields as noted here.
Rebuild/deploy via qt android build. Test on device. For most notes apps, “adjustPan” kills the overlap instantly. But if your layout still jitters on cursor moves? Layer on dynamic checks.
Fix 2: Dynamic Resizing with QInputMethod
“adjustPan” stabilizes, but for pixel-perfect control—especially with QTextEdit’s rich text or multi-line edits—monitor the keyboard yourself. Qt’s QInputMethod gives real-time intel, even in Widgets apps.
In your Widget class, connect to input method changes:
// In widget.h
private slots:
void onInputMethodChanged();
// In widget.cpp constructor
connect(QGuiApplication::inputMethod(), &QInputMethod::keyboardRectangleChanged,
this, &Widget::onInputMethodChanged);
connect(QGuiApplication::inputMethod(), &QInputMethod::visibleChanged,
this, &Widget::onInputMethodChanged);
// Implementation
void Widget::onInputMethodChanged() {
QInputMethod *im = QGuiApplication::inputMethod();
if (im->isVisible()) {
QRect kbRect = im->keyboardRectangle();
// Resize layout to screen height minus kb height
int kbHeight = kbRect.height();
resize(width(), height() - kbHeight); // Or adjust viewport
child->setFixedHeight(height() * 0.8); // Constrain QTextEdit
} else {
// Restore full size
resize(width(), height() + /* previous kb */);
}
}
This grabs the keyboard’s screen rect straight from Qt examples. Poll on visibleChanged() too. Your QVBoxLayout shrinks proportionally—no more under-keyboard text. Pairs beautifully with “adjustPan.”
Pro tip: Throttle with a QTimer if changes spam during typing.
Handling Layouts with Multiple Widgets
Your repro has one QTextEdit, but real apps stack buttons, labels, lists. All vanish under keyboard on edit?
Wrap smarter. Use a scrollable parent:
// Upgrade your constructor
QScrollArea *scrollArea = new QScrollArea();
scrollArea->setWidgetResizable(true);
QWidget *scrollWidget = new QWidget();
layout = new QVBoxLayout(scrollWidget);
layout->addWidget(new QLabel("Title"));
layout->addWidget(child); // QTextEdit
layout->addWidget(new QPushButton("Save"));
scrollArea->setWidget(scrollWidget);
setLayout(new QVBoxLayout(this));
layout()->addWidget(scrollArea);
Now, QScrollArea eats the resize hits. Combine with QInputMethod: on keyboard show, scrollArea->ensureWidgetVisible(child);. Widgets stay accessible, QTextEdit scrolls without expanding.
Seen this in qt 6 android builds—Qt6.4+ handles input better, but still needs nudges per forum gripes.
Advanced: Custom Keyboard Height Detection via JNI
If QInputMethod lags (rare, but happens on older Android), go native. JNI callbacks pipe keyboard height to Qt.
Follow this JNI approach—adapt for Widgets:
- In Java (AndroidManifest side): Register JNI for
onConfigurationChanged. - C++ bridge:
extern "C" void AndroidVirtualKeyboardStateChanged(jint height); - Emit Qt signal:
QMetaObject::invokeMethod(rootObject, "onKeyboardHeight", Q_ARG(int, height));
Slot in Widget:
slots:
void onKeyboardHeight(int height);
void Widget::onKeyboardHeight(int height) {
if (height > 0) {
// Shrink layout
child->setMaximumHeight(height() - height - 50); // Margin
}
}
Overkill for notes apps, but bulletproof for pyqt qtextedit ports or complex UIs. Needs qt android toolchain setup.
Testing and Best Practices
- Deploy:
adb installor Qt Creator’s Android kit. Test on physical devices—emulators fake keyboard heights. - Edge cases: Landscape, split-screen, multi-window. QTextEdit’s
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);. - Qt versions: Qt6 shines for “настройка qt для android,” but pin inputMode in pro file:
android:windowSoftInputMode=adjustPan. - Avoid: Fullscreen flags—they ignore keyboards.
- Debug:
qDebug() << im->keyboardRectangle();logs rects.
Users searching “QTextEdit keyboard overlap” swear by this stack: Manifest first, QInputMethod second.
Sources
- Resize QML window when the virtual keyboard is shown
- Android virtual keyboard input trouble
- QML: Resize controls when Android virtual keyboard come up
- Android soft keyboard resize layout
- Virtual keyboard or onscreen keyboard for QtWidgets applications
- Qt6.4 QLineEdit and Virtual Keyboard Difficulties On Android
Conclusion
Nail qt android QTextEdit keyboard overlaps with “adjustPan” in AndroidManifest.xml for 80% fixes, then QInputMethod for the rest—your notes app stays usable across edits. Skip JNI unless scaling to enterprise. Test ruthlessly on real hardware, and you’ll dodge those frantic StackOverflow dives. Solid layouts win users.