How to find the window that contains the QtVirtualKeyboard

你离开我真会死。 提交于 2021-02-02 09:40:35

问题


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.


回答1:


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(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()



回答2:


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.



来源:https://stackoverflow.com/questions/63955568/how-to-find-the-window-that-contains-the-qtvirtualkeyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!