QT做软键盘时将软键盘窗口控件放到指定控件的合适位置

本文介绍了一个用于将控件(如虚拟键盘等GUI组件)智能停靠到输入对象附近的方法。该方法通过计算输入对象的位置及大小,结合屏幕尺寸,确保控件出现在合理且不超出屏幕边界的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//功能:将控件停靠到指定窗口的合适位置
BOOL CQTGUINumKeyboard::moveTo(QWidget *inputObject)
{
    if(inputObject == NULL) return FALSE;

    QPoint pointGlobal(inputObject->mapToGlobal(QPoint(0,0)));//获取控件的屏幕坐标

    int objHeight = inputObject->height();
    int objWidth  = inputObject->width();
    qDebug()<<"objHeight"<<objHeight;
    qDebug()<<"objWidth"<<objWidth;

    int selfHeight = this->height();
    int selfWidth  = this->width();
    int screenWidth  = QApplication::desktop()->width();
    int screenHeight = QApplication::desktop()->height();

    int leftBottomPointX = pointGlobal.x();//移动到左下方坐标
    int leftBottomPointY = pointGlobal.y()+objHeight;
    int leftBottomRectWidth  = leftBottomPointX+selfWidth;
    int leftBottomRectHeight = leftBottomPointY+selfHeight;
    qDebug()<<"pointGlobal.x"<<pointGlobal.x();
    qDebug()<<"pointGlobal.y"<<pointGlobal.y();
    qDebug()<<"leftBottomPointX"<<leftBottomPointX;
    qDebug()<<"leftBottomPointY"<<leftBottomPointY;

    int leftTopPointX = pointGlobal.x();//移动到左上方坐标
    int leftTopPointY = pointGlobal.y()-selfHeight;
    int leftTopRectWidth = leftTopPointX+selfWidth;
    int leftTopRectHeight = leftTopPointY;

    int rightTopPointX = pointGlobal.x()+objWidth-selfWidth;//移动到右上方坐标
    int rightTopPointY = pointGlobal.y()-selfHeight;
    int rightTopRectWidth  = rightTopPointX;
    int rightTopRectHeight = rightTopPointY;

    int rightBottomPointX = pointGlobal.x()+objWidth-selfWidth;//移动到右下方坐标
    int rightBottomPointY = pointGlobal.y()+objHeight;
    int rightBottomRectWidth = rightBottomPointX;
    int rightBottomRectHeight = rightBottomPointY+selfHeight;

    if((leftBottomRectWidth<=screenWidth)&&(leftBottomRectHeight<=screenHeight))
        this->move(leftBottomPointX,leftBottomPointY);
    else if((leftTopRectWidth<=screenWidth)&&(leftTopRectHeight>=0))
        this->move(leftTopPointX,leftTopPointY);
    else if((rightTopRectWidth>=0)&&(rightTopRectHeight>=0))
        this->move(rightTopPointX,rightTopPointY);
    else if((rightBottomRectWidth>=0)&&(rightBottomRectHeight<=screenHeight))
        this->move(rightBottomPointX,rightBottomPointY);
    return TRUE;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值