如何找到包含 QtVirtualKeyboard 的窗口

Kub*_*ula 3 c++ qt qtvirtualkeyboard

我在嵌入式设备上使用 qt 小部件,但虚拟键盘有问题。键盘显示为全屏并与所有应用程序重叠。

在Yocto 中的虚拟键盘顶部黑屏一文中描述了如何解决此问题。

简而言之,你需要用键盘找到QQuickWindow,并在这个窗口上调用setMask。然后键盘上方的区域就会变成透明的

我在如何使用虚拟键盘查找 QQuickWindow 时遇到问题。我尝试使用

QApplication::allWidgets()
Run Code Online (Sandbox Code Playgroud)

但窗户不在这里。

eyl*_*esc 10

要获取您可以使用的所有窗口,QGuiApplication::allWindows()但这还不够,因为 QtVirtualKeyboard 窗口不一定是在开始时创建的,因此必须使用 QInputMethod 的visibleChanged 信号。我没有使用 QQuickWindow 中的信息进行过滤,因为通常应用程序可以有其他信息,而是使用窗口所属的类的名称。

#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);
    // ...
Run Code Online (Sandbox Code Playgroud)

Python版本:

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()
Run Code Online (Sandbox Code Playgroud)