如何滚动到 QML TextArea 中的行?
How to scroll to line in QML TextArea?
如何滚动到多行富文本 QML TextArea 中的特定行?有 positionToRectangle 但它只接受一个位置作为 int 似乎不适合多行文本。
我建议尝试根据行号乘以行高来计算位置。您可以这样计算一行的高度:
(textArea.implicitHeight - 2 * textArea.textMargin) / textArea.lineCount
这可能有助于您使用 positionToRectangle
。
编辑:事后想想,你试过设置cursorPosition
吗?
A TextArea
本身不可滚动(参见 Scrollable TextArea):
If you want to make a TextArea scrollable, [...] it can be placed inside a ScrollView.
然后你只需要设置 contentItem
的 contentY
:
ScrollView {
id: view
width: parent.width
height: 60
TextArea {
text: "A\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL"
}
}
Button {
text: "scroll"
onClicked: view.contentItem.contentY += 10
}
如何滚动到多行富文本 QML TextArea 中的特定行?有 positionToRectangle 但它只接受一个位置作为 int 似乎不适合多行文本。
我建议尝试根据行号乘以行高来计算位置。您可以这样计算一行的高度:
(textArea.implicitHeight - 2 * textArea.textMargin) / textArea.lineCount
这可能有助于您使用 positionToRectangle
。
编辑:事后想想,你试过设置cursorPosition
吗?
A TextArea
本身不可滚动(参见 Scrollable TextArea):
If you want to make a TextArea scrollable, [...] it can be placed inside a ScrollView.
然后你只需要设置 contentItem
的 contentY
:
ScrollView {
id: view
width: parent.width
height: 60
TextArea {
text: "A\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL"
}
}
Button {
text: "scroll"
onClicked: view.contentItem.contentY += 10
}