Qtvirtualkeyboard only working in main window not in additional windows

59 views Asked by At

I'm running a PyQt5 (5.15.9) application with Python 3.11.2 on a Raspberry Pi 4B (Raspberry Pi OS 64-bit Debian Bookworm) with connected touchscreen.

Qtvirtualkeyboard is working perfectly fine on all text inputs of my main window. However, I have an additional window, that opens when a button is clicked, but qtvirtualkeyboard is not working on the text input fields of this second window. When a text input field is focussed, the keyboard shows up, but is not responsive nor is any text inputted when keys are pressed.

I followed this answer to install qtvirtualkeyboard and used the example in this answer to include it in my application.

Minimal example of my code (the function "handleVisibleChanged" is used as suggested in the linked answer above):

import sys
import os
from PyQt5 import QtCore, QtGui, QtWidgets, uic

def handleVisibleChanged():
    if not QtGui.QGuiApplication.inputMethod().isVisible():
        return
    for window in QtGui.QGuiApplication.allWindows():
        if window.metaObject().className() == "QtVirtualKeyboard::InputView":
            keyboard = window.findChild(QtCore.QObject, "keyboard")
            if keyboard is not None:
                area = window.geometry()
                area.moveTop(int(keyboard.property("y")))
                window.setMask(QtGui.QRegion(area))
                return

class MyApp(QtWidgets.QMainWindow):
    '''Main window'''

    def __init__(self):
        super(MyApp, self).__init__()
        uic.loadUi('ui-file.ui', self)
        self.second_window = SecondWindow()
        self.button_for_second_window.clicked.connect(self.second_window.show)
        self.show()

class SecondWindow(QtWidgets.QDialog):
    '''Second window that opens when a button in MyApp is clicked'''
    
    def __init__(self):
        super(SecondWindow, self).__init__()
        # Load second window UI, that has QLineEdit
        uic.loadUi('second-ui-file.ui', self)
        self.setWindowModality(Qt.ApplicationModal)  # Disable main window when open

My app is run with the following piece of code (UI created with QtDesigner):

if __name__ == '__main__':
    os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
    app = QtWidgets.QApplication(sys.argv)
    QtGui.QGuiApplication.inputMethod().visibleChanged.connect(handleVisibleChanged)
    ex = MyApp()
    sys.exit(app.exec_())

What am I missing here? Any hints or suggestions are highly appreciated!

1

There are 1 answers

0
blue14 On

Found the problem: I had the following line in the instance for the second window:

self.setWindowModality(Qt.ApplicationModal)

This should disable the main window, while the second window is open. However, it also disables the qtvirtualkeyboard input, which is actually quite logical now....