簡體   English   中英

如何找到包含 QtVirtualKeyboard 的窗口

[英]How to find the window that contains the QtVirtualKeyboard

我在嵌入式設備上使用 qt 小部件,但虛擬鍵盤有問題。 鍵盤顯示為全屏並與所有應用程序重疊。

Yocto 中的虛擬鍵盤頂部黑屏一文中描述了如何解決這個問題。

總之,你需要用鍵盤找到QQuickWindow,並在這個窗口上調用setMask。 然后鍵盤上方的區域將是透明的

我在如何使用虛擬鍵盤查找 QQuickWindow 時遇到問題。 我試着用

QApplication::allWidgets()

但窗戶不在這里。

要獲取所有窗口,您可以使用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);
    // ...

蟒蛇版本:

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

您可以將findChildren與任何繼承QObject類一起使用,例如QApplication 例如在main.cpp 中

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

這將為您提供指向應用程序中所有QQuickWindow的指針列表。

我為將 Raspberry 與 PyQt5 一起使用的人提供了解決方案。 完成eyllanesc的回答,因為Python版本不支持PyQt5,其實我們有這個問題:

TypeError: setMask(self, QRegion): 參數 1 有意外的類型 'QRect'

要解決它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM