【问题标题】:How to show cursor on TextInput using forwardTo in QML如何在 QML 中使用 forwardTo 在 TextInput 上显示光标
【发布时间】:2014-04-13 05:48:24
【问题描述】:

我在屏幕上有两个元素,一个 Rectangle 和一个 TextInput。当在矩形上设置了 activefocus 并且我键入任何内容时,我需要将键入的输入输入到 TextInput

为此,我使用了curRect.Keys.forwardTo = [curTextbox];

它工作正常,但我也想在焦点位于矩形时看到光标。

http://qt-project.org/doc/qt-4.8/qml-textinput.html#cursorVisible-prop

在上面的QT链接中,它说

that 可以直接在脚本中设置,例如如果一个 KeyProxy 可能会将密钥转发给它,并且您希望它在这个时候看起来很活跃 发生(但实际上并没有给予它积极的关注)。

但在我的情况下不会发生这种情况。

谁能向我解释为什么会这样以及如何实现我的目标,即即使 activeFocus 在矩形上也能显示光标?

即使在输入文本时显示光标也很棒。

更新:

import QtQuick 1.0

FocusScope{

    width: 400
    height: 400
    TextInput {Rectangle{anchors.fill: parent;color:"#66ff0000"}
        id: mytext
        anchors.top:parent.top
        cursorVisible: true
        width: rect.width
        MouseArea{
            anchors.fill: parent;
            onClicked: mytext.forceActiveFocus()
        }
        Keys.onDownPressed: {
            mytext2.forceActiveFocus();
        }
    }
    TextInput {Rectangle{anchors.fill: parent;color:"#66ff0000"}
        id: mytext2
        anchors.top:mytext.bottom
        cursorVisible: true
        width: rect.width
        MouseArea{
            anchors.fill: parent;
            onClicked: mytext2.forceActiveFocus()
        }

        Keys.onUpPressed: {
            mytext.forceActiveFocus();
        }
    }
    Rectangle {
        id:rect
        width: 360
        height: 360
        color:rect.activeFocus?"#6600ff00":"#660000ff"
        focus: true
        Keys.forwardTo: mytext
        MouseArea{
            anchors.fill: parent;
            onClicked: rect.forceActiveFocus()
        }
    }
    Component.onCompleted: rect.forceActiveFocus();
}

提前致谢。

【问题讨论】:

  • 抱歉,这是不可能的。您可以随时输入 |在模糊上签名,这样输入中就会有一些东西。但它不会眨眼。在焦点上,您可以将光标移回管道所在的位置并将其删除。
  • @JakubPolomský 你确定吗?文档似乎表明它是。不过,我从未尝试过。
  • 我认为将活动焦点转移到 TextInput 可能更容易,为什么你没有选择?
  • 你是怎么做到的?我可以让它以最直接的方式工作。有关代码,请参阅我的答案。提供代码以便我们更好地帮助您。
  • 您好,请查看我添加的示例代码

标签: javascript qt qml


【解决方案1】:

焦点的改变重置cursorVisible属性...所以,当你点击rect,焦点从mytext通过forceActiveRecord转移到它,mytext.cursorVisible被自动设置为false。如果要保持光标可见,则应将其重置为 true。这可以通过将您的 rect.MouseArea.onClicked 更改为:

        onClicked: {
            rect.forceActiveFocus();
            mytext.cursorVisible = true;
        }

如果您希望mytext 上的光标始终可见,您还可以使用onCursorVisibleChanged 处理程序强制它为真。在您的 mytext 对象中使用它:

    onCursorVisibleChanged: {
        if (!cursorVisible)
            cursorVisible = true;
    }

PS:我正在发布一个新答案,因为添加的细节已经改变了很多......我可能稍后会删除以前的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    相关资源
    最近更新 更多