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

Windows 11平板Qt应用QSpinBox无法唤起系统虚拟键盘求助

Qt Widgets应用Windows 11平板下QSpinBox无法唤起虚拟键盘问题

我正在开发一款Qt Widgets应用,部署在Windows 11平板设备上。目前点击QLineEdit控件时,Windows 11内置虚拟键盘可正常弹出并编辑文本;但点击QSpinBox控件时,虚拟键盘无法唤起。应用必须以全屏模式运行,无法使用任务栏中的虚拟键盘快捷方式:
虚拟键盘快捷方式

已尝试的解决方案

  • 为QSpinBox设置QProxyStyle
  • 重写QEvent相关方法,通过QEvent e(QEvent::RequestSoftwareInputPanel);强制唤起虚拟键盘,代码如下:
bool event(QEvent *event) override
{
    if (QEvent::InputMethodQuery == event->type())
    {
        QEvent e(QEvent::RequestSoftwareInputPanel);
        return QApplication::sendEvent(qApp, &e);
    }
    return QSpinBox::event(event);
}
  • 使用QInputMethod设置setInputMethodHints(Qt::ImhDigitsOnly);
  • 查阅QLineEdit的源码

功能演示情况

  • 点击QLineEdit可唤起虚拟键盘:
    点击QLineEdit效果
  • 点击QSpinBox无任何反应:
    点击QSpinBox效果

当前使用Qt版本为6.5.0,完整源码如下:

touchscreen.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    custom.h \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

custom.h

#ifndef CUSTOM_H
#define CUSTOM_H


#include <QSpinBox>
#include <QFocusEvent>

#include <QApplication>


class Custom : public QSpinBox
{
    Q_OBJECT

public:
    Custom(QWidget* parent = 0) :
        QSpinBox(parent)
    {
    }

    void focusInEvent(QFocusEvent *event) override
    {
        qDebug() << event;

        QSpinBox::focusInEvent(event);
        QGuiApplication::inputMethod()->show();
    }
};

#endif // CUSTOM_H

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>



QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow

{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include <QApplication>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QProcess>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="1" column="1">
     <widget class="QLineEdit" name="lineEdit_2">
      <property name="readOnly">
       <bool>true</bool>
      </property>
     </widget>
    </item>
    <item row="1" column="0">
     <widget class="QLineEdit" name="lineEdit"/>
    </item>
    <item row="0" column="0">
     <widget class="Custom" name="spinBox"/>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <customwidgets>
  <customwidget>
   <class>Custom</class>
   <extends>QSpinBox</extends>
   <header>custom.h</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 04:14:55