Solved Problem with QtQuick VirtualKeyboard
-
Hi,
in my application I have a listview with controls like TextField and Spinner etc. inside delegates.
What I want is this: when the user taps on a TextField the VirtualKeyboard opens and the view should scroll so that the input item is not covered by it.I looked at the AutoScroller example in the documentation and tried to integrate something similar in my application but I always encounter the following problem.
Here is a simple example app with two TextFields.
main.cpp:
#include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard")); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); }
maini.qml:
import QtQuick 2.7 import QtQuick.Window 2.2 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.1 import QtQuick.VirtualKeyboard 2.1 Window { visible: true width: 640 height: 480 ColumnLayout { anchors.centerIn: parent spacing: 32 TextField{ text: qsTr("Field 1") } TextField{ text: qsTr("Field 2") } } function itemChanged() { if (InputContext.inputItem && Qt.inputMethod.visible) { console.log("CURSOR.Y = " + InputContext.cursorRectangle.y); } } Connections { target: InputContext onInputItemChanged: itemChanged() } }
The idea is to find the screen coordinates of the current cursor. Then map those coordinates to the ListView's coordinate space and to then scroll the view by setting its contentY property accordingly.
But I'm struggling with the first step of getting the cursor's screen coordinates.
It's demonstrated in this simple example. When the user activates one of the TextFields, the cursor position will always be 0 or incorrect the first time. Only an activation of another TextField will from then on give correct results.
Additionally when using the TextFields in a ListView delegate like I want to do, the cursor position will often reset to 0 after a little bit of scrolling.
Any ideas?
-
Sorry, just found a solution. The signal "onInputItemChanged" was the problem. I changed it to "onFocusEditorChanged" and it works now.