5

I'm trying to figure out how to use a virtual keyboard for a touchscreen, using python 3.8 and PyQt5 on Windows.

I saw that Qt had his own plugin QtVirtualKeyboard. I pretty much followed what has been done in this link, first installing Qt 5.15 with the Virtual Keyboard support, and then setting up the environment variables.

A simple code example would be this :

import os
import sys

from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget

os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"


class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.line_edit = None
        self.init_ui()

    def init_ui(self):
        self.line_edit = QLineEdit()
        self.line_edit2 = QLineEdit()
        self.layout = QVBoxLayout()
        self.main_widget = QWidget()
        self.main_widget.setLayout(self.layout)
        self.layout.addWidget(self.line_edit)
        self.layout.addWidget(self.line_edit2)
        self.setCentralWidget(self.main_widget)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())

The idea would be to display the keyboard when touching the line edit. For now, the window is corretly displayed but no keyboard will pop out. I tried to set up ("QT_DIR", "QT_PLUGIN_PATH",...) as in the link above, but nothing worked.

I know I'm missing something there but can't figure out what. Thank you for your help !

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Thecles
  • 53
  • 1
  • 1
  • 6
  • Have you compiled qtvirtualkeyboard? – eyllanesc Jun 19 '20 at 16:27
  • @eyllanesc I honestly thought it was automatically done when installing the plugin throught the Qt executable. I'm looking on how to compile it (I forgot to mention I'm new to Qt) and let you know how it goes... – Thecles Jun 22 '20 at 07:34
  • @eyllanesc I compiled qtvirtualkeyboard through the Qt Creator (did it with MingGW and MSVC without any error), and ran a "make install". I can easily use the keyboard with the C++ code example, however, not with PyQt5, nothing shows up with the little script above. – Thecles Jun 22 '20 at 13:45

2 Answers2

12

First, be the binaries associated with Qt VirtualKeyboard, and to not install Qt I have used aqtinstall(In this example Qt 5.15 was installed but it is recommended to use the same version that was used to compile pyqt5: python -c "from PyQt5.QtCore import QT_VERSION_STR; print('Qt version', QT_VERSION_STR)"):

python -m pip install aqtinstall
python -m aqt install 5.15.0 windows desktop win64_msvc2019_64 -m qtvirtualkeyboard --outputdir qt

Then it is located where the PyQt5 prefix path is:

python -c "from PyQt5.QtCore import QLibraryInfo; print('QT_PREFIX_PATH:', QLibraryInfo.location(QLibraryInfo.PrefixPath))"

Output:

QT_PREFIX_PATH: C:/Users/eyllanesc/qt_env/lib/site-packages/PyQt5/Qt

Then you have to copy the following from the folder where Qt was installed (the folder is called qt) to the prefix path (which is obtained with the previous command) of PyQt5:

  • Copy "qt/5.15.0/msvc2019_64/bin/Qt5VirtualKeyboard.dll" file to "QT_PREFIX_PATH/bin" folder.
  • Create the folder "QT_PREFIX_PATH/plugins/platforminputcontexts".
  • Copy "qt/5.15.0/msvc2019_64/plugins/platforminputcontexts/qtvirtualkeyboardplugin.dll" file to "QT_PREFIX_PATH/plugins/platforminputcontexts" folder.
  • Copy "qt/5.15.0/msvc2019_64/plugins/virtualkeyboard" folder to "QT_PREFIX_PATH/plugins" folder.
  • Copy "qt/5.15.0/msvc2019_64/qml/QtQuick/VirtualKeyboard" folder to "QT_PREFIX_PATH/qml/QtQuick" folder.

enter image description here

For linux it is similar:

python -m aqt install 5.15.0 linux desktop -m qtvirtualkeyboard --outputdir qt

Then

  • Copy "qt/5.15.0/gcc_64/bin/libQt5VirtualKeyboard.so.5" file to "QT_PREFIX_PATH/lib" folder.
  • Create the folder "QT_PREFIX_PATH/plugins/platforminputcontexts".
  • Copy "qt/5.15.0/gcc_64/plugins/platforminputcontexts/libqtvirtualkeyboardplugin.so" file to "QT_PREFIX_PATH/plugins/platforminputcontexts" folder.
  • Copy "qt/5.15.0/gcc_64/plugins/virtualkeyboard" folder to "QT_PREFIX_PATH/plugins" folder.
  • Copy "qt/5.15.0/gcc_64/qml/QtQuick/VirtualKeyboard" folder to "QT_PREFIX_PATH/qml/QtQuick" folder.
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Thank you very much for your help, it works ! Well documented answer. – Thecles Jun 23 '20 at 09:01
  • 2
    Thanks man, was struggling with this too. Just one little correction, I followed the linux steps and I think the 1. step has a typo. The bin folder doesn't contain .so files, they are located in the lib folder. So for linux you have to change the bin path to lib, And the libQt5VirtualKeyboard.so.5 is a symlink so we need to copy the file it points to too (libQt5VirtualKeyboard.so.5.14.0 in my case). – Muhammed B. Aydemir Aug 31 '20 at 08:42
  • On Raspberry Pi (Raspbian 10 Buster), latest qt version is 5.11.3, QT_PREFIX_PATH is /usr and there are no plugins and qml directories. Do you know where should I copy the files to? – ramiwi Jul 12 '21 at 17:54
  • 1
    @ramiwi See https://stackoverflow.com/questions/63719347/install-qtvirtualkeyboard-in-raspberry-pi/63720177#63720177 – eyllanesc Jul 12 '21 at 17:56
0

I am not really familiar with qvirtualkeyboard, but I could tell you the easiest way to do it manually. There is a module called getkey which can be installed using the pip install getkey command. With it you can handle key presses more dynamically. With a bit of work, you can create a series of buttons which when clicked trigger some keycodes, e.g:

key = getkey()
btn = QPushButton("UP Arrow Button")
btn.clicked.connect(someFunc)

And then...

def someFunc:
    key = keys.UP

And voila! If you click it, it acts like an upwards arrow key. With a bit more creativity, you could only make the keyboard pop-up when a QLineEdit is on focus. Just a few ideas... Hope this helped!

NSO2008
  • 21
  • 3