C++ 如何管理与子QML TextEdit相关的QML ScrollView导航?

C++ 如何管理与子QML TextEdit相关的QML ScrollView导航?,c++,qt,qml,qtquick2,C++,Qt,Qml,Qtquick2,我有一个内线。我有一个函数,我在选择a中的一行以跳转到TextEdit中所需的行时调用该函数,这很好。但是,我需要调整ScrollView焦点,使其在选择TextEdit时移动。 与在IDE上选择编译错误的行为相同 //main.qml ScrollView { id: palGenTextScrollView anchors.fill: parent TextEdit { id: mainTextEdit text: fileio.p

我有一个内线。我有一个函数,我在选择a中的一行以跳转到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
                        }
                    }
                }
            }
        }
    }
}
窗口{
可见:正确
宽度:640
身高:480
标题:qsTr(“你好世界”)
滚动视图{
id:palGenTextScrollView
宽度:200
身高:100
文本编辑{
id:mainTextEdit
正文:"我有一个文本编辑\n在滚动视图\n在分割视图\n中。\n我有一个Q\u可调用\n函数,当表格视图中的一行被选中跳转到文本编辑中所需的行\n时,我会调用该函数,\n这很好。\n但是,我需要调整\n滚动视图焦点,\n以便在文本编辑的选择移动时\n它会移动。\n诱惑行为\n选择正在调试IDE上的编译错误。“
wrapMode:TextEdit.Wrap
selectByMouse:true
}
}
划船{
间距:5
anchors.top:palGenTextScrollView.bottom
上边距:20
中继器{
型号:mainTextEdit.lineCount
代表:矩形{
宽度:20
身高:20
颜色:“蓝色”
正文{
anchors.centerIn:父对象
文本:索引
}
鼠耳{
锚定。填充:父级
再次点击:{
var lines=mainTextEdit.text.split(“\n”);
var计数=0;

对于(var i=0;i,您需要使用
palGenTextScrollView.flickableim.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
                        }
                    }
                }
            }
        }
    }
}
窗口{
可见:正确
宽度:640
身高:480
标题:qsTr(“你好世界”)
滚动视图{
id:palGenTextScrollView
宽度:200
身高:100
文本编辑{
id:mainTextEdit
文本:“我有一个文本编辑\n在滚动视图\n在分割视图\n中。\n我有一个Q\u可调用\n函数,当表格视图中的一行被选中跳转到文本编辑中所需的行\n时,我会调用该函数,\n这很好。\n但是,我需要调整\n滚动视图焦点,\n以便在文本编辑的选择移动时\n它会移动。\n诱惑行为\n选择正在调试IDE上的编译错误。“
wrapMode:TextEdit.Wrap
selectByMouse:true
}
}
划船{
间距:5
anchors.top:palGenTextScrollView.bottom
上边距:20
中继器{
型号:mainTextEdit.lineCount
代表:矩形{
宽度:20
身高:20
颜色:“蓝色”
正文{
anchors.centerIn:父对象
文本:索引
}
鼠耳{
锚定。填充:父级
再次点击:{
var lines=mainTextEdit.text.split(“\n”);
var计数=0;
对于(var i=0;i