Kubikula
Kubikula

Reputation: 33

How to find the window that contains the QtVirtualKeyboard

I'm using qt widgets on embedded device and have problem with virtual keyboard. Keyboard is shown as fullscreen and overlaps all app.

In article Virtual keyboard top black screen in Yocto is described hack how to solve this issue.

In short, you need to find the QQuickWindow with the keyboard and call setMask on this window. Then the area above the keyboard will be transparent

I have problem how to find QQuickWindow with virtual keyboard. I tried to use

QApplication::allWidgets()

but the window isn't here.

Upvotes: 3

Views: 2737

Answers (3)

eyllanesc
eyllanesc

Reputation: 244369

To obtain all the windows you can use QGuiApplication::allWindows() but that is not enough since the QtVirtualKeyboard window is not necessarily created at the beginning, so the visibleChanged signal of the QInputMethod must be used. I did not filter using the information from the QQuickWindow since in general the application could have others, instead it uses the name of the class to which the window belongs.

#include <QApplication>
#include <QWindow>
#include <cstring>

static void handleVisibleChanged(){
    if (!QGuiApplication::inputMethod()->isVisible())
        return;
    for(QWindow * w: QGuiApplication::allWindows()){
        if(std::strcmp(w->metaObject()->className(), "QtVirtualKeyboard::InputView") == 0){
            if(QObject *keyboard = w->findChild<QObject *>("keyboard")){
                QRect r = w->geometry();
                r.moveTop(keyboard->property("y").toDouble());
                w->setMask(r);
                return;
            }
        }
    }
}

int main(int argc, char *argv[])
{
    qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
    QApplication a(argc, argv);
    QObject::connect(QGuiApplication::inputMethod(), &QInputMethod::visibleChanged, &handleVisibleChanged);
    // ...

Python version:

import os
import sys

from PySide2 import QtCore, QtGui, QtWidgets
# from PyQt5 import QtCore, QtGui, QtWidgets


def handleVisibleChanged():
    if not QtGui.QGuiApplication.inputMethod().isVisible():
        return
    for w in QtGui.QGuiApplication.allWindows():
        if w.metaObject().className() == "QtVirtualKeyboard::InputView":
            keyboard = w.findChild(QtCore.QObject, "keyboard")
            if keyboard is not None:
                r = w.geometry()
                r.moveTop(keyboard.property("y"))
                w.setMask(QtGui.QRegion(r))
                return


def main():
    os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
    app = QtWidgets.QApplication(sys.argv)

    QtGui.QGuiApplication.inputMethod().visibleChanged.connect(handleVisibleChanged)

    w = QtWidgets.QLineEdit()
    w.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

Upvotes: 10

charlyf
charlyf

Reputation: 11

I have a solution for people who use Raspberry with PyQt5. To complete the answer of eyllanesc because the Python version does not work with PyQt5, in fact, we have this problem :

TypeError: setMask(self, QRegion): argument 1 has unexpected type 'QRect'

To solve it :

def handleVisibleChanged():
    if not QtGui.QGuiApplication.inputMethod().isVisible():
        return
    for w in QtGui.QGuiApplication.allWindows():
        if w.metaObject().className() == "QtVirtualKeyboard::InputView":
            keyboard = w.findChild(QtCore.QObject, "keyboard")
            if keyboard is not None:
                region = w.mask()
                rect = [w.geometry()]
                rect[0].moveTop(keyboard.property("y"))
                region.setRects(rect)
                w.setMask(region)
                return

Upvotes: 1

Chaitanya
Chaitanya

Reputation: 175

You can use findChildren with any class that inherits QObject such as QApplication. For example in main.cpp:

QApplication a(argc, argv);
QList<QQuickWindow *> wind = a.findChildren<QQuickWindow *>();

This will give you a list of pointers pointing to all the QQuickWindow in your application.

Upvotes: 0

Related Questions