在对话框中显示虚拟键盘

Show VirtualKeyboard in Dialog

我在 qml 中使用 qtvirtualkeyboard 模块。我使用以下 qml 代码来显示虚拟键盘。

import QtQuick 2.5
import QtQuick.VirtualKeyboard 2.1

    InputPanel {
        id: inputPanel
        y: Qt.inputMethod.visible ? parent.height - inputPanel.height : parent.height
        anchors.left: parent.left
        anchors.right: parent.right
        focus: true
    }

当我在模态配置为真的对话框中调用该 qml 时,我无法触摸键盘。如果对话框模式配置为 false,那么我可以触摸键盘,但这次对话框是隐藏的。我还希望用户只能控制对话框屏幕上的键盘。

如何在对话框屏幕上控制虚拟键盘?

如果我对问题的理解正确,那么这很可能与 QTBUG-56918 是同一个问题。正如 JP 在该错误报告的评论中提到的那样,一种可能的解决方法(对于 Qt Quick Controls 2 应用程序)是在 InputPanel 上设置 parent: root.overlayz: 1 以将其提升到弹出窗口之上(或对话框)。

如果您想要针对多个不同对话框的可重用解决方案,将键盘作为对话框的子项会给您带来压力。因此,我的解决方法是使用一个后面有 MouseArea 的非模态对话框,它可以被实例化并用作对话框(但使用别名属性而不是 Item 的属性):

ModalDialog.qml:

Item {
  anchors.fill: parent
  property alias title: dialog.title
  property alias _x: dialog.x
  property alias _y: dialog.y
  property alias _width: dialog.width
  property alias _height: dialog.height
  property alias closePolicy: dialog.closePolicy
  property alias standardButtons: dialog.standardButtons
  default property alias contentData: dialog.contentData
  property alias _visible: dialog.visible
  visible: _visible
  function open() { dialog.open() }

  Dialog {
    id: dialog
  }
  MouseArea {
    anchors.fill: parent
    z: 100
  }
  Rectangle {
    anchors.fill: parent
    color: "black"
    opacity: dialog.opacity / 2
    z: 100
  }
}

为 InputPanel 设置以下 属性:

z: 1
parent: Overlay.overlay
focus: true

并为 Popup 设置以下 属性:

modal: false
focus: true