The previous article introduced Qt5 Virtual Keyboard compilation and size settings, now let’s take a look at how to make Qt keyboard implementation based on input controls Position and size while automatically adjusting your own position.
Qt5 Virtual Keyboard C++ Integration and Implementation II (Adaptive Position)
I. Implementation
-
inputcontex.h adds the following:
Q_PROPERTY(QRectF inputItemGeometry READ inputItemGeometry) QRectF inputItemGeometry();
inputcontex.cpp增加如下内容:
QRectF InputContext::inputItemGeometry() { QWidget* pInputItem = (QWidget*)inputItem(); return pInputItem ? QRectF(((QWidget*)pInputItem->parent())->mapToGlobal(pInputItem->geometry().topLeft()), pInputItem->geometry().size()) : QRectF(0,0,0,0); }
We use this function to get the position and size information of the current control.
-
InputPanel.qml add the following:
anchors.horizontalCenter: parent.horizontalCenter width: Screen.desktopAvailableWidth * 2 / 3 states: State { name: "visible"; when: keyboard.active; PropertyChanges { target: keyboard; y: getInputY() } } transitions: Transition { from: ""; to: "visible"; reversible: true; ParallelAnimation { NumberAnimation { properties: "y"; duration: 250; easing.type: Easing.InOutQuad; } } } function getInputY(){ if(InputContext.inputItemGeometry.y + InputContext.inputItemGeometry.height + keyboard.height <= screenHeight){ return InputContext.inputItemGeometry.y + InputContext.inputItemGeometry.height } else if(InputContext.inputItemGeometry.y - keyboard.height - 100 >= 0) { return InputContext.inputItemGeometry.y - keyboard.height - 50 } else { return screenHeight - keyboard.height } }
We use the getInputY function to adjust the position of the Qt keyboard based on the position and size of the input control.
II. Effects
III. More Qt5 Virtual Keyboard Settings
- Qt5 Virtual Keyboard C++ Integration and Implementation I (Multilingual Keyboard Compilation and Custom Size Based on QWidget)
- Qt5 Virtual Keyboard C++ Integration and Implementation III (Solving the Problem of Modal Dialog Keyboard Invalidation)