我找不到虚拟键盘的尺寸。下面是一个示例:
Rectangle {
Component.onCompleted: {
Qt.inputMethod.visibleChanged.connect(resizeForKeyboard)
}
Component.onDestruction: {
Qt.inputMethod.visibleChanged.disconnect(resizeForKeyboard)
}
function resizeForKeyboard(){
console.log('Visibility changed!!!');
var keys = Object.keys(Qt.inputMethod.keyboardRectangle);
var rect = Qt.inputMethod.keyboardRectangle;
//A simple script I have for debugging, this loops
//through the keys and prints all properties
DataMethods.printObject(keys, '[INPUT]');
DataMethods.printObject(rect , '[RECTANGLE]');
}
//using the controls to save time
TextField {
focus: true //force keyboard to show up.
}
}
数据方法.js(相关方法)
/**
* This method prints an object to the console for debug purposes
* obj -> the objec to print
* prefix -> the prefix to append "[prefix] -> "...
* props -> a list of properties to use, otherwiese all will be printed
*/
function printObject(obj, prefix, props) {
if(!prefix)
prefix = "obj";
if(obj){
console.log(prefix + obj + "->" + typeof obj);
if(props){
for(var p in obj)
console.log('t' + prefix + "["+ p + "] -> '" + obj[p] + "'");
} else {
for(var p in obj)
console.log('t' + prefix + "["+ p + "] -> '" + obj[p] + "'");
}
} else {
console.log(prefix + "is null");
}
}
下面是输出:
[INPUT]objectName,cursorRectangle,keyboardRectangle,visible,animating,locale,inputDirection,destroyed,destroyed,objectNameChanged,deleteLater,_q_reregisterTimers,cursorRectangleChanged,keyboardRectangleChanged,visibleChanged,animatingChanged,localeChanged,inputDirectionChanged,show,hide,update,reset,commit,invokeAction->object
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][0] -> 'objectName'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][1] -> 'cursorRectangle'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][2] -> 'keyboardRectangle'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][3] -> 'visible'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][4] -> 'animating'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][5] -> 'locale'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][6] -> 'inputDirection'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][7] -> 'destroyed'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][8] -> 'destroyed'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][9] -> 'objectNameChanged'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][10] -> 'deleteLater'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][11] -> '_q_reregisterTimers'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][12] -> 'cursorRectangleChanged'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][13] -> 'keyboardRectangleChanged'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][14] -> 'visibleChanged'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][15] -> 'animatingChanged'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][16] -> 'localeChanged'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][17] -> 'inputDirectionChanged'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][18] -> 'show'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][19] -> 'hide'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][20] -> 'update'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][21] -> 'reset'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][22] -> 'commit'
D/Qt (30122): qrc:js/datamethods.js:153 (printObject): [INPUT][23] -> 'invokeAction'
D/Qt (30122): qrc:js/datamethods.js:147 (printObject): [RECTANGLE]QRectF(0, 0, 0, 0)->object
我可能以完全错误的方式处理这个问题,我希望有一种处理它的方法。我需要设备上键盘大小的原因是,我可以在 UI 中响应,而不是将控件埋在它下面(以便用户可以继续滚动窗体)。
如您所见,Qt.inputMethod 的 keyboardRectangle
属性似乎是默认值。
我应该在哪里检索此信息,信号在打开/关闭虚拟键盘时触发时似乎是正确的?
我能够通过使用QDesktopWidget::availableGeometry();
来获取可用的屏幕尺寸来获取虚拟键盘尺寸,QDesktopWidget::screenGeometry();
获取全屏尺寸和一点 Java 代码来获取菜单栏大小。
以下是代码中最相关的部分:
C++面:
QRect KeyboardInterface::rect()
{
int menuheight = (int)QAndroidJniObject::callStaticMethod<jint>("org.qtproject.example.Demo2.JavaInterface", "getHeight");
QDesktopWidget widget;
QRect rect = widget.availableGeometry();
QRect geom = widget.screenGeometry();
rect.moveTop(rect.top() + menuheight);
geom.setTop(geom.top() + menuheight);
QRect final;
if (rect != geom)
{
int ftop, fleft, fwidth, fheight;
geom.getRect(&fleft, &ftop, &fwidth, &fheight);
if (rect.top() != ftop)
fheight = rect.top();
else if (rect.left() != fleft)
fwidth = rect.left();
else if (rect.height() != fheight)
ftop = rect.height();
else if (rect.width() != fwidth)
fleft = rect.width();
final = QRect(fleft, ftop, fwidth - fleft, fheight - ftop);
}
return final;
}
爪哇端:
package org.qtproject.example.Demo2;
import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.Window;
public class JavaInterface extends org.qtproject.qt5.android.bindings.QtActivity
{
private static JavaInterface instance;
public JavaInterface()
{
instance = this;
}
public static int getHeight()
{
Rect r = new Rect();
Window window = instance.getWindow();
View rootview = window.getDecorView();
rootview.getWindowVisibleDisplayFrame(r);
return r.top;
}
}
该项目的完整源代码可以在这里找到
希望有帮助。
对于5.6以下的所有Qt版本,这是Qt在Android上的预期行为。理由是Android上的软键盘不限于显示在屏幕底部
,就像iOS一样:https://bugreports.qt.io/browse/QTBUG-40731对于Qt 5.6及更高版本,该错误已修复,键盘矩形在Android上也返回有效的矩形。
OP看到的错误"Qt.inputMethod.keyboardRectangle
总是0 x 0"行为是Qt中的一个错误,该错误已在2015-12-07修复,该修复程序在Qt 5.7中发布。
从那时起,Qt.inputMethod.keyboardRectangle
将包含正确的大小,除了极少数浮动Android键盘的情况。在iOS下不会发生这种情况,其中键盘仅限于屏幕底部。这个限制现在也记录在官方的Qt文档中:
Q输入方法::键盘矩形:窗口坐标中的虚拟键盘几何图形。
如果无法知道键盘的几何形状,这可能是一个空矩形。安卓上的浮动键盘就是这种情况。