PyQt5 - 显示虚拟键盘 [英] PyQt5 - Show virtual keyboard

查看:409
本文介绍了PyQt5 - 显示虚拟键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何在触摸屏上使用虚拟键盘,在 Windows 上使用 python 3.8 和 PyQt5.

我看到 Qt 有自己的插件 QtVirtualKeyboard.我几乎遵循了

对于 linux 来说是类似的:

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

然后

  • 复制qt/5.15.0/gcc_64/bin/libQt5VirtualKeyboard.so.5"文件到QT_PREFIX_PATH/lib"文件夹.
  • 创建文件夹QT_PREFIX_PATH/plugins/platforminputcontexts".
  • 复制qt/5.15.0/gcc_64/plugins/platforminputcontexts/libqtvirtualkeyboardplugin.so";文件到QT_PREFIX_PATH/plugins/platforminputcontexts"文件夹.
  • 复制qt/5.15.0/gcc_64/plugins/virtualkeyboard"文件夹到QT_PREFIX_PATH/plugins"文件夹.
  • 复制qt/5.15.0/gcc_64/qml/QtQuick/VirtualKeyboard"文件夹到QT_PREFIX_PATH/qml/QtQuick"文件夹.

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 !

解决方案

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.

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.

这篇关于PyQt5 - 显示虚拟键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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