QML textInput如何处理选择和转义

时间:2016-03-23 08:53:33

标签: qt qml

我正在构建一个QML应用程序,我有一个input { jdbc { jdbc_connection_string => "jdbc:mysql://localhost/homestead" jdbc_user => "homestead" jdbc_password => "secret" jdbc_driver_library => "/home/vagrant/Code/mysql-connector-java-5.1.38-bin.jar" jdbc_driver_class => "com.mysql.jdbc.Driver" statement => "SELECT * from volunteer" add_field => {"type" => "volunteer"} } jdbc { jdbc_connection_string => "jdbc:mysql://localhost/homestead" jdbc_user => "homestead" jdbc_password => "secret" jdbc_driver_library => "/home/vagrant/Code/mysql-connector-java-5.1.38-bin.jar" jdbc_driver_class => "com.mysql.jdbc.Driver" statement => "SELECT * from contact" add_field => {"type" => "contact"} } } output { elasticsearch { hosts => ["localhost"] index => "homestead" document_type => "%{type}" <--- specify the index here document_id => "%{uid}" } } 组件,如下所示:

TextInput

目前,它的方式是当用户点击文本时,它选择整个文本,然后我可以开始输入,整个文本将被替换,但我希望用户给出更精细的控制。首先,用户最初选择全文,然后如果再次点击,它应该基本上将光标放在当前位置。此外,转义键基本上应该恢复旧文本并取消整个操作。

这些是文本输入的标准方式。我想知道是否必须明确地编写所有这些内容,或者是否有办法通过TextInput { id: editableTextInput text: styleData.value horizontalAlignment: Text.AlignLeft verticalAlignment: Text.AlignVCenter selectionColor: 'darkgray' selectedTextColor: 'white' font.pixelSize: 14 font.family: "AvantGarde-Medium" onEditingFinished: { // TO DO: Handle new text } MouseArea { anchors.fill: parent onClicked: { editableTextInput.selectAll() } } } 控件获得此行为。

1 个答案:

答案 0 :(得分:0)

基本上,您不需要使用MouseArea。挂钩activeFocus决定何时选择文本(在初始点击时,activeFocus将变为真),存储旧文本,并在编辑完成时恢复它,如果按下了转义。

我认为这可以帮助您获得所需的一部分:

import QtQuick 2.6

TextInput {
    text: "Hello world!" + index
    font.pixelSize: 24
    width: 300
    height: 30

    // Store the previous text for restoring it if we cancel
    property string oldText

    // Lets us know that the user is cancelling the save
    property bool cancelling

    Keys.onEscapePressed: {
        // Cancel the save, and deselect the text input
        cancelling = true
        focus = false
    }

    onEditingFinished: {
        if (cancelling) {
            // When cancelling, restore the old text, and clear state.
            text = oldText
            oldText = ""
            cancelling = false
        } else {
            // TO DO: Handle new text
        }
    }

    onActiveFocusChanged: {
        // When we first gain focus, save the old text and select everything for clearing.
        if (activeFocus) {
            oldText = text
            selectAll()
        }
    }
}