android 显示虚拟键盘时调整QML窗口大小

mfuanj7w  于 2023-10-14  发布在  Android
关注(0)|答案(5)|浏览(147)

我正在使用Qt/QML编写聊天应用程序。然而,我在Android设备上测试应用程序时发现了一个问题:虚拟键盘将窗口向上“移动”,不允许我看到许多显示的消息,只能看到我的应用程序的底部部分。
理想情况下,我想调整窗口的大小,以便同时显示消息控件(如文本框和附加文件按钮)和标题栏。对于一个图形示例,您可以看看这个:



在QML中可以做到这一点吗?

bhmjp9jg

bhmjp9jg1#

你可以让Android为你做这件事。
Android将调整您的应用程序窗口,每当虚拟键盘显示后,您调整AndroidManifest.xml<activity>标签如下:

  1. <activity ... android:windowSoftInputMode="adjustResize">
  • 来源:* 这是在两条关于Qt bug的评论中讨论的一种解决方法,该bug阻止手动关闭窗口一段时间,直到2015年底。
yzuktlbb

yzuktlbb2#

从Qt 5.12部署到Android 10(C++,不需要QML)。似乎没有任何非QML C++的例子可以响应屏幕上键盘可见性的变化。我发现的那些需要从Qt 4与w/ Java接口。
有必要为所有可见的UI创建一个与QMainWindow分离的容器。QMainWindow通常占据整个屏幕,并与屏幕键盘重叠。容器QWidget是 * 可以 * 调整大小的,并且必须包含您希望不在键盘下的每个UI元素。
该示例使用QFrame作为一个非常小的(轻量级)容器。
YourApp.cpp:

  1. YourApp::YourApp ( QWidget *parent ) : QMainWindow ( parent ) {
  2. // With Android, an application running normally ...
  3. // ... occupies the whole screen. Plan accordingly.
  4. QSize availableSize = qApp->desktop()->availableGeometry().size();
  5. Application_Width = availableSize.width();
  6. Application_Height = availableSize.height();
  7. App_Frame = new QFrame(this);
  8. // Build your UI inside this QFrame
  9. setCentralWidget(App_Frame);
  10. Virtual_Keyboard_Enabled = true;
  11. App_Input_Method = QApplication::inputMethod();
  12. connect(App_Input_Method, SIGNAL(keyboardRectangleChanged()),
  13. this, SLOT(onKeyboardRectangleChanged()));
  14. this->show();
  15. }
  16. void
  17. YourApp::onKeyboardRectangleChanged ( ) {
  18. #if defined(Q_OS_ANDROID)
  19. bool keyboard_visible = App_Input_Method->isVisible();
  20. QRectF keyboard_rectangle = App_Input_Method->keyboardRectangle();
  21. if (not keyboard_visible) {
  22. App_Frame->resize(Application_Width, Application_Height);
  23. }
  24. else {
  25. int keyboard_height = int(keyboard_rectangle.height());
  26. App_Frame->resize(Application_Width,
  27. (Application_Height - keyboard_height));
  28. }
  29. #endif
  30. }
  31. void
  32. YourApp::Toggle_Virtual_Keyboard_Enabled ( ) {
  33. #if defined(Q_OS_ANDROID)
  34. Virtual_Keyboard_Enabled = not Virtual_Keyboard_Enabled;
  35. App_Input_Method->setVisible(Virtual_Keyboard_Enabled);
  36. qApp->setAutoSipEnabled(Virtual_Keyboard_Enabled);
  37. #endif
  38. }

YourApp.h:

  1. class YourApp : public QMainWindow {
  2. Q_OBJECT
  3. public:
  4. YourApp ( QWidget *parent = nullptr );
  5. ~YourApp ( );
  6. private:
  7. bool Virtual_Keyboard_Enabled;
  8. QInputMethod *App_Input_Method;
  9. QFrame *App_Frame;
  10. void
  11. Toggle_Virtual_Keyboard_Enabled ( );
  12. private slots:
  13. void
  14. onKeyboardRectangleChanged ( );
  15. }
展开查看全部
nhaq1z21

nhaq1z213#

这篇文章解释了当Android虚拟键盘出现时调整QML控件大小的方法。它涉及到一些java代码的使用,但你可以直接复制和传递与项目示例链接提供的代码:
QML: Resize controls when Android virtual keyboard come up

krugob8w

krugob8w4#

当用户显示或隐藏虚拟键盘时,有一种仅限QML的React方式,即重新显示窗口内容。
首先,确保窗口关闭不是由Android为您完成的(这也是可能的)。因此,您可以告诉Android键盘应该与窗口重叠,方法是调整AndroidManifest.xml中的<activity>标记,如下所示:

  1. <activity ... android:windowSoftInputMode="adjustPan">

然后,将以下内容放入QML文件中,您可以访问要调整大小和/或重新定位的窗口或窗口内容:

  1. Connections {
  2. target: Qt.inputMethod
  3. onKeyboardRectangleChanged: {
  4. var newRect = Qt.inputMethod.keyboardRectangle
  5. console.log(
  6. "New keyboard rectangle size:" +
  7. " x: " + newRect.x +
  8. " y: " + newRect.y +
  9. " width: " + newRect.width +
  10. " height: " + newRect.height
  11. )
  12. // Your UI resizing / repositioning code goes here.
  13. }
  14. }

说明和细节:

  • Qt QML类型是不可示例化的(源),因此不能写入Qt { inputMethod.onKeyboardRectangleChanged: { }}
  • Connections QML类型是为这些情况而设计的,允许在发出它的对象外部实现信号处理程序(详细信息)。
  • 另一种方法是使用connect()将信号连接到一个JavaScript函数,如here所示。
  • 在底层C++类QInputMethod中使用的QRectF类型在QML中作为QML基本类型rect提供。这是记录here

当与C集成时,请注意,从C传递到QML的任何QRect或QRIF值都会自动转换为rect值,反之亦然。

  • 您不应该在onVisibleChanged信号处理程序中实现此事件,因为当用户单击“hide keyboard”按钮时,该事件在Android上不会被触发。(在Android 6.0和Qt 5.12上测试)这似乎是Qt中的一个bug,因为高度为0的键盘肯定是不可见的。
展开查看全部
agyaoht7

agyaoht75#

  1. TextArea {
  2. //..
  3. //.. work with qt 6.5.0 on iOS
  4. //..
  5. Connections {
  6. target: Qt.inputMethod
  7. function onKeyboardRectangleChanged() {
  8. var newRect = Qt.inputMethod.keyboardRectangle
  9. console.log(
  10. "New keyboard rectangle size:" +
  11. " x: " + newRect.x +
  12. " y: " + newRect.y +
  13. " width: " + newRect.width +
  14. " height: " + newRect.height
  15. )
  16. // Your UI resizing / repositioning code goes here.
  17. }
  18. }

}

展开查看全部

相关问题