Qt5 Virtual Keyboard C++ Integration and Implementation II (Adaptive Position)

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

  1. 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.

  2. 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

Qt5 Virtual Keyboard C++ Integration and Implementation II (Adaptive Position)

III. More Qt5 Virtual Keyboard Settings

  1. Qt5 Virtual Keyboard C++ Integration and Implementation I (Multilingual Keyboard Compilation and Custom Size Based on QWidget)
  2. Qt5 Virtual Keyboard C++ Integration and Implementation III (Solving the Problem of Modal Dialog Keyboard Invalidation)
0 0 votes
Article Rating
赞(3) 打赏
未经允许不得转载:iemblog » Qt5 Virtual Keyboard C++ Integration and Implementation II (Adaptive Position)
Subscribe
Notify of
guest

0 COMMENTS
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
免责声明:本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有,请在下载后24小时之内自觉删除,若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系站长删除,我们将及时处理! Disclaimer: Most of the download resources on this site are collected on the Internet, and are only used for learning and communication. The copyright belongs to the original author. Please consciously delete within 24 hours after downloading. If you use it for commercial purposes, please purchase the original version. If the content posted on this site violates your rights, please contact us to delete it, and we will deal with it in time!

联系我们 Contact us

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

0
Would love your thoughts, please comment.x
()
x