简体   繁体   English

Qt/QML Android 3rd 方虚拟键盘不适用于 TextInput

[英]Qt/QML Android 3rd party virtual keyboard not working with TextInput

it seems that 3rd party virtual keyboards on android are not working properly when deploying a simple test app with Qt 5.2.1?使用 Qt 5.2.1 部署简单的测试应用程序时,Android 上的第 3 方虚拟键盘似乎无法正常工作? I've tested with all items that can receive text input, always the same result (TextInput, TextEdit and even TextField and TextArea) I am using the SwiftKey Keyboard on my android devices and I can only type 1 char and the next key press replaces the whole text (even if there is more than 1 char before I press a key), also when pressing the space key it appears a random key and no space, very weird.我已经测试了所有可以接收文本输入的项目,结果总是相同(TextInput、TextEdit 甚至 TextField 和 TextArea)我在我的 android 设备上使用SwiftKey 键盘,我只能输入 1 个字符,下一个按键替换整个文本(即使在我按下一个键之前有超过 1 个字符),当按下空格键时,它会出现一个随机键并且没有空格,非常奇怪。 with the default android keyboard there are no problem as far as I am aware of, but 3rd party keyboards are widely used on android I think, so that might be a problem.使用默认的 android 键盘,据我所知没有问题,但我认为 3rd 方键盘在 android 上被广泛使用,所以这可能是一个问题。

Is that a known bug or am I missing something?这是一个已知的错误还是我遗漏了什么?

when setting “inputMethodHints: Qt.ImhNoPredictiveText” it works better, I can type stuff but space is still not working and also I would like dictionary suggestions :) Since I don't have any other 3rd party keyboards I don't know if the problem is only with SwiftKey, but that is one of the best keyboards in the store.设置“inputMethodHints: Qt.ImhNoPredictiveText”时效果更好,我可以输入内容,但空格仍然不起作用,我还需要字典建议:) 因为我没有任何其他 3rd 方键盘,我不知道问题仅在于 SwiftKey,但那是商店中最好的键盘之一。

Code example:代码示例:

import QtQuick 2.2

Rectangle {
    width: 360
    height: 360

    TextInput {
        anchors.fill: parent
        font.pointSize: 20
        text: "type here"
        //inputMethodHints: Qt.ImhNoPredictiveText
    }
}

also I get these warnings in the console output:我也在控制台输出中收到这些警告:

W/IInputConnectionWrapper( 8703): getExtractedText on inactive InputConnection
W/IInputConnectionWrapper( 8703): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper( 8703): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper( 8703): getTextAfterCursor on inactive InputConnection

It should be fixed in Qt 5.3.0 RC1 .它应该在Qt 5.3.0 RC1 中修复。 At present Qt 5.3 has been released, so it's worth a try.目前Qt 5.3已经发布,值得一试。

I am currently using QT5.5.1 for a android application and there is still a issue with the TextInput control capturing certain keyboard events.我目前正在将 QT5.5.1 用于 android 应用程序,并且 TextInput 控件捕获某些键盘事件仍然存在问题。

Originally I was displaying my QML in a QQuickWidget what was part of a qt widgets based gui.最初我在 QQuickWidget 中显示我的 QML,这是基于 qt 小部件的 gui 的一部分。 The problem I ran into was that QML TextInput items were not receiving the keypress events from the android keyboard.我遇到的问题是 QML TextInput 项目没有从 android 键盘接收按键事件。

namespace Ui {
class MainWindow;
}


class MainWindow : public QMainWindow
{

===================================================================== ================================================== ====================

#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QFile file(":/styles/app_style.css");
    file.open(QFile::ReadOnly);
    QString styleSheet = QString::fromLatin1(file.readAll());
    a.setStyleSheet(styleSheet);
    MainWindow w;
    w.statusBar()->setHidden(true);
    w.show();
    return a.exec();
}

Note: Don't waste time exploring focus or FocusScope.注意:不要浪费时间探索 focus 或 FocusScope。

As a workaround I switched to a full QML application using the QQuickView which worked well except the TextInput still did not respond when numeric keys (0, 1, 2, 3, ..., 9) where pressed.作为一种解决方法,我使用 QQuickView 切换到完整的 QML 应用程序,该应用程序运行良好,但在按下数字键(0、1、2、3、...、9)时 TextInput 仍然没有响应。

What was happening and what might be happening in your case, is that the keypress events for the numeric keys and perhaps third-party keyboards keypress events are being picked up by the QQuickView keyPressEvent handler.正在发生的事情以及在您的情况下可能发生的事情是,QQuickView keyPressEvent 处理程序正在接收数字键的按键事件以及可能是第三方键盘的按键事件。

======mainwindow.h====================================================== ======mainwindow.h========================================== ==============

class MainWindow : public QQuickView
{
    Q_OBJECT

public:
    explicit MainWindow(QWindow  *parent = 0);
    ~MainWindow();
    void keyPressEvent(QKeyEvent *event);

======mainwindow.cpp=================================================== ======mainwindow.cpp========================================== ==========

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Back)
    {
        qDebug() << "[[Back button]]";

======main.cpp========================================================= ======main.cpp========================================== ================

int main(int argc, char *argv[])
{
    QGuiApplication app(argc,argv);
    MainWindow view;
    view.show();
    return app.exec();
}

====================================================================== ================================================== ====================

I'm not a Qt Expert but I've faced with this problem this week.我不是 Qt 专家,但本周我遇到了这个问题。 As much as I googled I found that its a bug, but I've managed to solve the problem.正如我在谷歌上搜索的那样,我发现它是一个错误,但我已经设法解决了这个问题。 Here is my approach, you can implement similar key events on your own.这是我的方法,您可以自己实现类似的关键事件。

 Item {
        Layout.fillHeight: true
        Layout.fillWidth: true
        id: rectDomain

        Rectangle {
            anchors.fill: parent
            anchors.margins: 2 * Screen.pixelDensity
            border.width: 1
            border.color: "#916E34"

            TextEdit {
                id: txtDomain
                property int lastCursorIndex: 0
                anchors.fill: parent
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignVCenter
                font.pointSize: 16
                text: controller.domain //"192.168.20.57"
                inputMethodHints: Qt.ImhNoPredictiveText
                //                    wrapMode: TextEdit.WrapAnywhere
                //                    color: "#0B151E"
                focus: true
                Keys.onReleased: {
                    if (event.key == Qt.Key_Backspace)
                    {
                        txtDomain.lastCursorIndex = txtDomain.selectionStart
                        console.log("Selection Test... Selection Start : " + txtDomain.lastCursorIndex + "Selection End" + txtDomain.selectionEnd)
                        if (txtDomain.selectionStart == txtDomain.selectionEnd) // Remove single character
                        {
                            text = text.substring(0, cursorPosition-1) + text.substring(cursorPosition, text.length)
                            cursorPosition = txtDomain.lastCursorIndex- 1
                        }
                        else                                                        // Remove selected part from text.
                        {

                            text = text.substring(0, txtDomain.selectionStart) + text.substring(txtDomain.selectionEnd, text.length)
                            cursorPosition =  txtDomain.lastCursorIndex
                        }

                    }
                    else
                        console.log("It is not the Backspce... Might be Space Enter UppoerCase, Change Language Button")

                }
            }

        }

    }

Just a Hint, in my approach i need to select part before get rid of them.只是一个提示,在我的方法中,我需要在摆脱它们之前选择部分。 So I needed to set focus:true所以我需要设置 focus:true

Hope this will help you, Sincerely.希望这会帮助你,真诚的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM