简体   繁体   中英

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? 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. 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.

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.

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 . At present Qt 5.3 has been released, so it's worth a try.

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.

Originally I was displaying my QML in a QQuickWidget what was part of a qt widgets based gui. The problem I ran into was that QML TextInput items were not receiving the keypress events from the android keyboard.

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.

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.

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.

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

class MainWindow : public QQuickView
{
    Q_OBJECT

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

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

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

======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. 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

Hope this will help you, Sincerely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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