[英]How to manage QML ScrollView navigation with respect to child QML TextEdit?
I have a TextEdit inside a Scrollview inside a SplitView . 我在SplitView的Scrollview内有一个TextEdit 。 I have a Q_INVOKABLE function that I call when a row in a TableView gets selected to jump to a desired line in the TextEdit, this works fine. 我有一个Q_INVOKABLE函数,当选择TableView中的一行以跳到TextEdit中的所需行时,我会调用它,这可以正常工作。 However, I need to adjust the ScrollView focus so that it moves when the selection of the TextEdit moves. 但是,我需要调整ScrollView焦点,以使其在选择TextEdit时移动。 Identical behavior to selecting a compiling error on an IDE. 与选择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. ??
}
I omitted some irrelevant code. 我省略了一些无关的代码。
You need to use palGenTextScrollView.flickableItem.contentY
to set the position of your text. 您需要使用palGenTextScrollView.flickableItem.contentY
设置文本的位置。 Here after is a small example working: you have a button for each line of the text, and clicking on it selects the line and centers the text on it. 以下是一个小示例工作:您为文本的每一行都有一个按钮,然后单击以选择该行并将文本居中。 You can work on it for your own issue. 您可以针对自己的问题进行处理。
I couldn't manage to have your example working because the palerrorviewmodel
element is missing. 我无法使您的示例正常工作,因为缺少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.