我有一些qml作爲應用程序的輸出(類似於只讀控制檯)。然而,它使用起來很煩人,因爲它打印的信息會滾動回頂端。例如,可以說我每秒都會在我的TextArea
上打印一行,一分鐘左右之後,我將有足夠的線條,現在我有一個滾動條。我滾動到底部,稍後,打印一行文字,使其滾動回頂部。請記住QML元素中的滾動位置
我想要的功能是自動滾動到底部(很像控制檯在打印內容時的樣子),除非用戶通過向上滾動覆蓋此內容,則文本應該保持放置狀態。我希望是有道理的,這裏是一些代碼:
ScrollArea {
id: helpTextScrollArea
anchors.left: parent.left
anchors.right: parent.right
anchors.top: myButton.bottom
anchors.topMargin: 5
anchors.bottom: parent.bottom
visible: false
horizontalScrollBar.visible: false
onVisibleChanged: {
helpText.visible = visible
}
HelpTextArea {
id: helpText
width: parent.parent.width - helpTextScrollArea.verticalScrollBar.width
text: "Oops, no documentation."
onVisibleChanged: {
if(helpTextScrollArea.visible != visible) {
helpTextScrollArea.visible = visible
}
}
}
}
Flickable
{
id: flick
anchors.left: parent.left
anchors.right: parent.right
anchors.top: runStopButton.bottom
anchors.bottom: parent.bottom
anchors.topMargin: 5
TextArea{
id: messageArea
anchors.fill: parent
focus: true
readOnly: true
visible: !helpTextScrollArea.visible
wrapMode: TextEdit.Wrap
function addText(newText) {
text += newText + "\n"
}
}
}
注:我不認爲我的Flickable做任何事情,這是我的實驗來解決這個問題的一部分。另外,我使用功能addText
打印到文本區域。我幾乎不知道有關qml的任何內容,而且大部分代碼都是由其他人編寫的,我正在嘗試使用它。謝謝!