You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下PyQt5+Python3.8使用QtVirtualKeyboard无弹出问题求助

Troubleshooting Qt Virtual Keyboard Not Popping Up on Touch

Let’s work through this issue step by step—here are the most common fixes to get your virtual keyboard working with PyQt5 on Windows:

1. Verify Plugin Path Configuration

First, make sure Qt can locate the virtual keyboard plugin. Set the QT_PLUGIN_PATH to the plugins folder of your Qt 5.15 installation. For example, if you installed Qt 5.15.2 with MSVC 2019 64-bit:

os.environ["QT_PLUGIN_PATH"] = r"C:\Qt\5.15.2\msvc2019_64\plugins"

Double-check that the path exists and contains the platforminputcontexts/qtvirtualkeyboard.dll file (this is the actual virtual keyboard plugin for Windows).

2. Confirm Environment Variable Timing & Values

You’re on the right track setting QT_IM_MODULE before importing PyQt5 components, but let’s verify it isn’t being overridden. Add a print statement right after setting it to confirm:

os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
print(os.environ.get("QT_IM_MODULE"))  # Should output "qtvirtualkeyboard"

Also, add a virtual keyboard style setting to ensure the plugin initializes correctly:

os.environ["QT_VIRTUALKEYBOARD_STYLE"] = "default"

3. Force Touch Mode & High-DPI Support

Windows might not detect your application as touch-aware by default. Add platform arguments to simulate touch mode, and enable high-DPI scaling to avoid touch input misalignment:

if __name__ == "__main__":
    # Add touch platform flag
    sys.argv.extend(["-platform", "windows:touchscreen"])
    
    app = QApplication(sys.argv)
    # Enable high-DPI settings
    app.setAttribute(Qt.AA_EnableHighDpiScaling)
    app.setAttribute(Qt.AA_UseHighDpiPixmaps)
    
    # Rest of your code...

4. Check Version Compatibility

Ensure your PyQt5 version is compiled against the same Qt 5.15 version you installed. Print these values to confirm:

from PyQt5.QtCore import QT_VERSION_STR, PYQT_VERSION_STR
print(f"Qt Version: {QT_VERSION_STR}")
print(f"PyQt5 Version: {PYQT_VERSION_STR}")

If the Qt version here doesn’t match your installed 5.15.x, reinstall PyQt5 to match (e.g., pip install pyqt5==5.15.4 aligns with Qt 5.15.4).

5. Debug Plugin Loading

If nothing works, enable Qt’s plugin debug output to see why the virtual keyboard isn’t loading. Add this line before creating the QApplication:

os.environ["QT_DEBUG_PLUGINS"] = "1"

Run your script and check the console for errors related to qtvirtualkeyboard—common issues include missing dependencies (like VC++ redistributables) or incorrect plugin paths.

Modified Test Code

Here’s a consolidated version of your code with all the fixes above:

import os
import sys
from PyQt5.QtWidgets import QApplication, QLineEdit, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt

# Configure environment variables FIRST
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
# Replace with your actual Qt 5.15 installation path
os.environ["QT_PLUGIN_PATH"] = r"C:\Qt\5.15.2\msvc2019_64\plugins"
os.environ["QT_VIRTUALKEYBOARD_STYLE"] = "default"
# Uncomment to debug plugin loading
# os.environ["QT_DEBUG_PLUGINS"] = "1"

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.init_ui()

    def init_ui(self):
        self.line_edit = QLineEdit()
        self.line_edit.setPlaceholderText("Touch to show keyboard")
        self.line_edit2 = QLineEdit()
        self.line_edit2.setPlaceholderText("Second input field")
        
        layout = QVBoxLayout()
        main_widget = QWidget()
        main_widget.setLayout(layout)
        layout.addWidget(self.line_edit)
        layout.addWidget(self.line_edit2)
        
        self.setCentralWidget(main_widget)
        self.setWindowTitle("Virtual Keyboard Test")
        self.resize(400, 300)

if __name__ == "__main__":
    # Enable touch platform mode
    sys.argv.extend(["-platform", "windows:touchscreen"])
    
    app = QApplication(sys.argv)
    # Fix high-DPI touch alignment
    app.setAttribute(Qt.AA_EnableHighDpiScaling)
    app.setAttribute(Qt.AA_UseHighDpiPixmaps)
    
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())

Final Checks

  • Ensure your Windows touch settings are enabled (Go to Settings > System > Touch).
  • Close any third-party input method editors (IMEs) that might interfere with Qt’s virtual keyboard.

内容的提问来源于stack exchange,提问作者Thecles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 07:17:27