簡體   English   中英

如何針對子QML TextEdit管理QML ScrollView導航?

[英]How to manage QML ScrollView navigation with respect to child QML TextEdit?

我在SplitViewScrollview內有一個TextEdit 我有一個Q_INVOKABLE函數,當選擇TableView中的一行以跳到TextEdit中的所需行時,我會調用它,這可以正常工作。 但是,我需要調整ScrollView焦點,以使其在選擇TextEdit時移動。 與選擇IDE上的編譯錯誤相同的行為。

//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. ??
}

我省略了一些無關的代碼。

您需要使用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
                        }
                    }
                }
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM