How to find the window that contains the QtVirtualKeyboard

后端 未结 2 1285
暖寄归人
暖寄归人 2020-12-22 00:37

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

相关标签:
2条回答
  • 2020-12-22 01:02

    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.

    0 讨论(0)
  • 2020-12-22 01:14

    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()
    
    0 讨论(0)
提交回复
热议问题