2016-10-13 35 views
0

私はTextEditの中にScrollviewの中にSplitViewを持っています。 TableViewの行がTextEditの目的の行にジャンプするように選択されたときに呼び出すQ_INVOKABLE関数があります。これは正常に動作します。しかし、TextEditが選択されたときに移動するように、ScrollViewフォーカスを調整する必要があります。 IDEでコンパイルエラーを選択するのと同じ動作です。QML ScrollViewナビゲーションを子QML TextEditに関して管理するには?

//main.qml 

ScrollView { 
    id: palGenTextScrollView 
    anchors.fill: parent 

    TextEdit { 
     id: mainTextEdit 
     text: fileio.palFileText 
     wrapMode: TextEdit.Wrap 
     selectByMouse: true 
    } 

TableView { 
    id: errorsTableView 
    onClicked: { 
     mainTextEdit.select(palerrorviewmodel.goToLine(errorsTableView.currentRow), 
          palerrorviewmodel.goToLine(errorsTableView.currentRow)) 
     mainTextEdit.forceActiveFocus() 
     //Call something to adjust ScrollView here 
     //palGenTextScrollView. ?? 
} 

私はいくつかの無関係のコードを省略しました。

答えて

1

palGenTextScrollView.flickableItem.contentYを使用してテキストの位置を設定する必要があります。ここでは、小さな例を示します。テキストの各行にボタンがあり、それをクリックすると、その行が選択され、テキストが中央に配置されます。あなた自身の問題のためにそれに取り組むことができます。

palerrorviewmodel要素が不足しているため、あなたのサンプルが正常に動作しませんでした。

Window { 
    visible: true 
    width: 640 
    height: 480 
    title: qsTr("Hello World") 

    ScrollView { 
     id: palGenTextScrollView 
     width: 200 
     height: 100 

     TextEdit { 
      id: mainTextEdit 
      text: "I have a TextEdit\ninside a Scrollview\ninside a SplitView.\nI have a Q_INVOKABLE\nfunction that I call\nwhen a row in a TableView\ngets selected to jump\nto a desired line\nin the TextEdit,\nthis works fine.\nHowever, I need to adjust\nthe ScrollView focus\nso that it moves\nwhen the selection\nof the TextEdit moves.\nIdentical behavior\nto selecting a compiling\nerror on an IDE." 
      wrapMode: TextEdit.Wrap 
      selectByMouse: true 
     } 
    } 

    Row{ 
     spacing: 5 
     anchors.top: palGenTextScrollView.bottom 
     anchors.topMargin: 20 

     Repeater{ 
      model: mainTextEdit.lineCount 

      delegate: Rectangle{ 
       width: 20 
       height: 20 
       color: "blue" 
       Text{ 
        anchors.centerIn: parent 
        text: index 
       } 

       MouseArea{ 
        anchors.fill: parent 
        onClicked: { 
         var lines = mainTextEdit.text.split("\n"); 
         var count=0; 
         for (var i=0; i<index;i++){ 
          count+=(lines[i].length+1); 
         } 
         mainTextEdit.select(count, count+lines[index].length); 
         mainTextEdit.forceActiveFocus() 

         var maxY = mainTextEdit.contentHeight-palGenTextScrollView.height 
         var lineHeight = mainTextEdit.contentHeight/mainTextEdit.lineCount 
         var centeredY=index*lineHeight-palGenTextScrollView.height/2 
         if (centeredY < 0){ 
          palGenTextScrollView.flickableItem.contentY=0 
         }else if (centeredY<=maxY){ 
          palGenTextScrollView.flickableItem.contentY=centeredY 
         }else{ 
          palGenTextScrollView.flickableItem.contentY=maxY 
         } 
        } 
       } 
      } 
     } 
    } 
} 
関連する問題