当文本过长时自动将文字以滚动的形式播放。
文件导入
通过设置一个宽度值,当文本过长时自动滚动。
TextScroll { width: 300 fontPixelSize: 40 text: "遇见你每天都有好心情,没关系薯片辣条都给你。"}
text:string
设置显示的文本。fontPixelSize:int
设置字体像素大小。color:string
设置文本颜色。width:real
设置文本的宽度,默认为文本宽度。如果设置的宽度小于文本的宽度则自动开启文本滚动。height:real
设置文本的高度,默认为文本高度。你才小学生
(nicaixiaoxuesheng)import QtQuick 2.0
Item {
id: root
property string text: "text"
property int fontPixelSize: 20
property string color: "black"
width: sourceText.width
height: sourceText.height
clip: true
PathView {
id: pathView
visible: sourceText.width > root.width
width: textLoader.width*2; height: textLoader.height
model: 2
delegate: textComponent
path: Path {
startX: 0; startY: pathView.height/2
PathLine { x: pathView.width; y: pathView.height/2 }
}
NumberAnimation on x {
id: animation
loops: Animation.Infinite
from: 0
to: -textLoader.width
duration: 5000
}
}
Text {
id: sourceText
visible: width <= root.width
text: root.text
font.pixelSize: root.fontPixelSize
color: root.color
}
Loader {
id: textLoader
visible: false
sourceComponent: textComponent
}
Component {
id: textComponent
Text {
text: root.text + " "
font.pixelSize: root.fontPixelSize
color: root.color
}
}
/* Shielding sliding events. */
MouseArea { anchors.fill: parent }
}