2016-03-23 4 views
0

を逃れるために、私はQMLアプリケーションを構築していますし、次のように私はTextInputコンポーネントを持っている:QMLに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() 
        } 
       } 
      } 

を現時点では、ユーザーがテキストをクリックしたとき、それは立っ方法は、ありますテキスト全体を選択した後、入力を開始するとテキスト全体が置き換えられますが、ユーザーにはさらに細かい制御を与えて欲しいのです。最初は、ユーザーが最初に全文を選択し、再度クリックすると、基本的に現在の位置にカーソルを置きます。また、エスケープキーは基本的に古いテキストを復元し、操作全体をキャンセルする必要があります。

これはテキスト入力の標準的な方法です。私はこれを明示的にすべてプログラムする必要があるのか​​、TextInputコントロールでこの動作を取得する方法があるのだろうかと思いました。

+2

あなたがキャプチャするキーイベントとMouseAreaをキャッチするキーQMLモジュールを使用して、それをカスタマイズするためにそれをコーディングする必要がクリック – Cits

答えて

0

基本的に、これにはMouseAreaを使用する必要はありません。フックactiveFocusを使用して、テキストを選択するとき(最初のクリック時にactiveFocusがtrueになる)、古いテキストを保存し、エスケープが押されたときに編集が完了したら復元します。

私は、これはあなたが望むものへの道の良い部分を取得すると思う:

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() 
     } 
    } 
} 
関連する問題