一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

一. 实现

inputcontex.h增加如下内容:

1 Q_PROPERTY(QRectF inputItemGeometry READ inputItemGeometry)
2 QRectF inputItemGeometry();

inputcontex.cpp增加如下内容:

1 QRectF InputContext::inputItemGeometry()
2 {
3     QWidget* pInputItem = (QWidget*)inputItem();
4     return pInputItem ? QRectF(((QWidget*)pInputItem->parent())->mapToGlobal(pInputItem->geometry().topLeft()), pInputItem->geometry().size()) : QRectF(0,0,0,0);
5 }

我们使用这个函数来获取当前控件的位置和大小信息。

InputPanel.qml增加如下内容:

 1     anchors.horizontalCenter: parent.horizontalCenter
 2     width: Screen.desktopAvailableWidth * 2 / 3
 3 
 4     states: State {
 5         name: "visible";
 6         when: keyboard.active;
 7         PropertyChanges {
 8             target: keyboard;
 9             y: getInputY()
10         }
11     }
12     transitions: Transition {
13         from: "";
14         to: "visible";
15         reversible: true;
16         ParallelAnimation {
17             NumberAnimation {
18                 properties: "y";
19                 duration: 250;
20                 easing.type: Easing.InOutQuad;
21             }
22         }
23     }
24     function getInputY(){
25         if(InputContext.inputItemGeometry.y + InputContext.inputItemGeometry.height + keyboard.height <= screenHeight){
26             return InputContext.inputItemGeometry.y + InputContext.inputItemGeometry.height
27         }
28         else if(InputContext.inputItemGeometry.y - keyboard.height - 100 >= 0)
29         {
30             return InputContext.inputItemGeometry.y - keyboard.height - 50
31         }
32         else
33         {
34             return screenHeight - keyboard.height
35         }
36     }

我们使用getInputY函数根据输入控件的位置和大小来调整Qt键盘的位置。

二. 效果

posted on 2024-02-26 20:06  一杯清酒邀明月  阅读(82)  评论(0编辑  收藏  举报