我正試圖制作一個訊息合成器小部件。我想在ScrollView的高度改變時滾動到底部,以便用戶能夠跟上他們正在寫的內容。我怎樣才能實作這種功能呢?
以下是我正在使用的ScrollView的代碼:
ScrollView {
id: scrollView
anchors.left: parent.left
anchors.right: clearTextBtn.left
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.bottomMargin: 0。
anchors.leftMargin: 0 anchors.leftMargin: 010
夾子。true
ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
hoverEnabled: true ScrollBar.horizontal.policy: ScrollBar.AlwaysOff.
onHoveredChanged: {
if (hovered) {
borderWidth = 2 else {
borderWidth = focus ? 2 : 0 ?
cursorShape = Qt.ArrowCursor
}
}
onFocusChanged: {
if (focus) {
borderWidth = 2 {
} else {
borderWidth = 0 !
}
}
TextEdit {
id: textEdit
寬度: scrollView.width
horizontalAlignment: Text.AlignLeft
verticalAlignment: 垂直對齊。文本.對準頂部
anchors.bottomMargin: 0。
anchors.topMargin: 0 anchors.topMargin: 0
夾子。true
color: textColor
anchors.left: parent.left
wrapMode: Text.WordWrap
anchors.leftMargin: 0
padding: 10
selectByMouse: true
標簽 {
id: placeholderTxt
文本。qsTr("Compose Message...")
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
verticalAlignment: Text.AlignTop
anchors.topMargin: 10。
anchors.rightMargin: 223 錨點.右邊距:223
anchors.leftMargin: 10
visible: textEdit.length == 0 && !textEdit.activeFocus
color: "#a3a3a3"/span>
}
}
}
uj5u.com熱心網友回復:
試試這個 :
importQtQuick 2.12
importQtQuick.Window 2.12
importQtQuick.Controls 2.12
視窗 {
id: box
寬度:640
高度。180
可見。true true
標題。qsTr("ScrollBar"/span>)
可滑動 {
id: inputWrapper
anchors.fill: parent
contentHeight: input.implicitHeight
contentWidth: input.implicitWidth
ScrollBar.vertical: ScrollBar {
id: scrollBar
政策。ScrollBar.AlwaysOn
anchors.left: box.right
}
Keys.onUpPressed: scrollBar.decrease()
Keys.onDownPressed: scrollBar.increase()
夾子。true: true.
flickableDirection: Flickable.VerticalFlick
函式 ensureVisible(r)
{
if (contentX >= r.x)
contentX = r.x;
else if (contentX width <=r.x r.width)
contentX = r.x r.width-width。
if (contentY >= r.y)
contentY = r.y;
else if (contentY height <=r.y r.height)
contentY = r.y r.height-高度。
}
文本編輯器 {
id: 輸入
anchors.fill: parent
文本。""
焦點。true
wrapMode: TextEdit.Wrap
onCursorRectangleChanged: inputWrapper.ensureVisible(cursorRectangle)
} //TextEdit。
} // Flickable }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/310172.html
標籤:

