我正在使用Qt/QML编写聊天应用程序。然而,我在Android设备上测试应用程序时发现了一个问题:虚拟键盘将窗口向上“移动”,不允许我看到许多显示的消息,只能看到我的应用程序的底部部分。理想情况下,我想调整窗口的大小,以便同时显示消息控件(如文本框和附加文件按钮)和标题栏。对于一个图形示例,您可以看看这个:
。在QML中可以做到这一点吗?
bhmjp9jg1#
你可以让Android为你做这件事。Android将调整您的应用程序窗口,每当虚拟键盘显示后,您调整AndroidManifest.xml的<activity>标签如下:
AndroidManifest.xml
<activity>
<activity ... android:windowSoftInputMode="adjustResize">
yzuktlbb2#
从Qt 5.12部署到Android 10(C++,不需要QML)。似乎没有任何非QML C++的例子可以响应屏幕上键盘可见性的变化。我发现的那些需要从Qt 4与w/ Java接口。有必要为所有可见的UI创建一个与QMainWindow分离的容器。QMainWindow通常占据整个屏幕,并与屏幕键盘重叠。容器QWidget是 * 可以 * 调整大小的,并且必须包含您希望不在键盘下的每个UI元素。该示例使用QFrame作为一个非常小的(轻量级)容器。YourApp.cpp:
YourApp::YourApp ( QWidget *parent ) : QMainWindow ( parent ) { // With Android, an application running normally ... // ... occupies the whole screen. Plan accordingly. QSize availableSize = qApp->desktop()->availableGeometry().size(); Application_Width = availableSize.width(); Application_Height = availableSize.height(); App_Frame = new QFrame(this); // Build your UI inside this QFrame setCentralWidget(App_Frame); Virtual_Keyboard_Enabled = true; App_Input_Method = QApplication::inputMethod(); connect(App_Input_Method, SIGNAL(keyboardRectangleChanged()), this, SLOT(onKeyboardRectangleChanged())); this->show();}voidYourApp::onKeyboardRectangleChanged ( ) {#if defined(Q_OS_ANDROID) bool keyboard_visible = App_Input_Method->isVisible(); QRectF keyboard_rectangle = App_Input_Method->keyboardRectangle(); if (not keyboard_visible) { App_Frame->resize(Application_Width, Application_Height); } else { int keyboard_height = int(keyboard_rectangle.height()); App_Frame->resize(Application_Width, (Application_Height - keyboard_height)); }#endif}voidYourApp::Toggle_Virtual_Keyboard_Enabled ( ) {#if defined(Q_OS_ANDROID) Virtual_Keyboard_Enabled = not Virtual_Keyboard_Enabled; App_Input_Method->setVisible(Virtual_Keyboard_Enabled); qApp->setAutoSipEnabled(Virtual_Keyboard_Enabled);#endif}
YourApp::YourApp ( QWidget *parent ) : QMainWindow ( parent ) {
// With Android, an application running normally ...
// ... occupies the whole screen. Plan accordingly.
QSize availableSize = qApp->desktop()->availableGeometry().size();
Application_Width = availableSize.width();
Application_Height = availableSize.height();
App_Frame = new QFrame(this);
// Build your UI inside this QFrame
setCentralWidget(App_Frame);
Virtual_Keyboard_Enabled = true;
App_Input_Method = QApplication::inputMethod();
connect(App_Input_Method, SIGNAL(keyboardRectangleChanged()),
this, SLOT(onKeyboardRectangleChanged()));
this->show();
}
void
YourApp::onKeyboardRectangleChanged ( ) {
#if defined(Q_OS_ANDROID)
bool keyboard_visible = App_Input_Method->isVisible();
QRectF keyboard_rectangle = App_Input_Method->keyboardRectangle();
if (not keyboard_visible) {
App_Frame->resize(Application_Width, Application_Height);
else {
int keyboard_height = int(keyboard_rectangle.height());
App_Frame->resize(Application_Width,
(Application_Height - keyboard_height));
#endif
YourApp::Toggle_Virtual_Keyboard_Enabled ( ) {
Virtual_Keyboard_Enabled = not Virtual_Keyboard_Enabled;
App_Input_Method->setVisible(Virtual_Keyboard_Enabled);
qApp->setAutoSipEnabled(Virtual_Keyboard_Enabled);
YourApp.h:
class YourApp : public QMainWindow { Q_OBJECTpublic: YourApp ( QWidget *parent = nullptr ); ~YourApp ( );private: bool Virtual_Keyboard_Enabled; QInputMethod *App_Input_Method; QFrame *App_Frame; void Toggle_Virtual_Keyboard_Enabled ( );private slots: void onKeyboardRectangleChanged ( );}
class YourApp : public QMainWindow {
Q_OBJECT
public:
YourApp ( QWidget *parent = nullptr );
~YourApp ( );
private:
bool Virtual_Keyboard_Enabled;
QInputMethod *App_Input_Method;
QFrame *App_Frame;
Toggle_Virtual_Keyboard_Enabled ( );
private slots:
onKeyboardRectangleChanged ( );
nhaq1z213#
这篇文章解释了当Android虚拟键盘出现时调整QML控件大小的方法。它涉及到一些java代码的使用,但你可以直接复制和传递与项目示例链接提供的代码:QML: Resize controls when Android virtual keyboard come up
krugob8w4#
当用户显示或隐藏虚拟键盘时,有一种仅限QML的React方式,即重新显示窗口内容。首先,确保窗口关闭不是由Android为您完成的(这也是可能的)。因此,您可以告诉Android键盘应该与窗口重叠,方法是调整AndroidManifest.xml中的<activity>标记,如下所示:
<activity ... android:windowSoftInputMode="adjustPan">
然后,将以下内容放入QML文件中,您可以访问要调整大小和/或重新定位的窗口或窗口内容:
Connections { target: Qt.inputMethod onKeyboardRectangleChanged: { var newRect = Qt.inputMethod.keyboardRectangle console.log( "New keyboard rectangle size:" + " x: " + newRect.x + " y: " + newRect.y + " width: " + newRect.width + " height: " + newRect.height ) // Your UI resizing / repositioning code goes here. }}
Connections {
target: Qt.inputMethod
onKeyboardRectangleChanged: {
var newRect = Qt.inputMethod.keyboardRectangle
console.log(
"New keyboard rectangle size:" +
" x: " + newRect.x +
" y: " + newRect.y +
" width: " + newRect.width +
" height: " + newRect.height
)
// Your UI resizing / repositioning code goes here.
说明和细节:
Qt
Qt { inputMethod.onKeyboardRectangleChanged: { }}
Connections
connect()
QInputMethod
QRectF
rect
当与C集成时,请注意,从C传递到QML的任何QRect或QRIF值都会自动转换为rect值,反之亦然。
onVisibleChanged
agyaoht75#
TextArea {//..//.. work with qt 6.5.0 on iOS//..Connections { target: Qt.inputMethod function onKeyboardRectangleChanged() { var newRect = Qt.inputMethod.keyboardRectangle console.log( "New keyboard rectangle size:" + " x: " + newRect.x + " y: " + newRect.y + " width: " + newRect.width + " height: " + newRect.height ) // Your UI resizing / repositioning code goes here. }}
TextArea {
//..
//.. work with qt 6.5.0 on iOS
function onKeyboardRectangleChanged() {
5条答案
按热度按时间bhmjp9jg1#
你可以让Android为你做这件事。
Android将调整您的应用程序窗口,每当虚拟键盘显示后,您调整
AndroidManifest.xml
的<activity>
标签如下:yzuktlbb2#
从Qt 5.12部署到Android 10(C++,不需要QML)。似乎没有任何非QML C++的例子可以响应屏幕上键盘可见性的变化。我发现的那些需要从Qt 4与w/ Java接口。
有必要为所有可见的UI创建一个与QMainWindow分离的容器。QMainWindow通常占据整个屏幕,并与屏幕键盘重叠。容器QWidget是 * 可以 * 调整大小的,并且必须包含您希望不在键盘下的每个UI元素。
该示例使用QFrame作为一个非常小的(轻量级)容器。
YourApp.cpp:
YourApp.h:
nhaq1z213#
这篇文章解释了当Android虚拟键盘出现时调整QML控件大小的方法。它涉及到一些java代码的使用,但你可以直接复制和传递与项目示例链接提供的代码:
QML: Resize controls when Android virtual keyboard come up
krugob8w4#
当用户显示或隐藏虚拟键盘时,有一种仅限QML的React方式,即重新显示窗口内容。
首先,确保窗口关闭不是由Android为您完成的(这也是可能的)。因此,您可以告诉Android键盘应该与窗口重叠,方法是调整
AndroidManifest.xml
中的<activity>
标记,如下所示:然后,将以下内容放入QML文件中,您可以访问要调整大小和/或重新定位的窗口或窗口内容:
说明和细节:
Qt
QML类型是不可示例化的(源),因此不能写入Qt { inputMethod.onKeyboardRectangleChanged: { }}
。Connections
QML类型是为这些情况而设计的,允许在发出它的对象外部实现信号处理程序(详细信息)。connect()
将信号连接到一个JavaScript函数,如here所示。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的键盘肯定是不可见的。agyaoht75#
}