如何查找包含QtVirtualKeyboard的窗口 [英] How to find the window that contains the QtVirtualKeyboard

查看:102
本文介绍了如何查找包含QtVirtualKeyboard的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

简而言之,您需要使用键盘找到QQuickWindow并在此窗口上调用setMask.然后键盘上方的区域将是透明的

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

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

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

QApplication::allWidgets()

但是窗口不在这里.

推荐答案

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

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版本:

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

这篇关于如何查找包含QtVirtualKeyboard的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆