//在这里新建一个QML文件,文件名为MoveableText.qml
Item {
id: wrapper
width: 120; height: text.paintedHeight
clip: true
property alias text: text.text
property int fontSize: 24
property int duration: 2000
property alias color: text.color
property alias font: text.font
property bool marqueeEnable: true
property real targetLeftMost: wrapper.width - text.paintedWidth
property alias xpos: text.x
property int delayScroll: 0
property bool reset: false // add this for reset the text location
Text {
id: text
x: 0
anchors.verticalCenter: parent.verticalCenter
font.pixelSize: wrapper.fontSize
clip: true
// add this for reset the text location
SequentialAnimation on x {
running: wrapper.reset
loops: 1
NumberAnimation { to: 0; duration: 0 }
}
SequentialAnimation on x {
running: wrapper.marqueeEnable && (text.paintedWidth > wrapper.width)
loops: Animation.Infinite
PauseAnimation { duration: delayScroll }
NumberAnimation { to: wrapper.targetLeftMost; duration: wrapper.duration }
PauseAnimation { duration: 500 }
NumberAnimation { to: 0; duration: 0 }
PauseAnimation { duration: 500 }
}
}
}
// test code
Item {
width: 360
height: 360
focus:true
property int flag: 0
MarqueeText {
anchors.centerIn: parent
text: "Hello World......"
NumberAnimation on x { to: 50; duration: 1000 }
marqueeEnable:(flag == 0) ? true : false;
reset:(marqueeEnable == true) ? false : true;
}
MouseArea {
anchors.fill: parent
onClicked: {
if (flag == 1)
{
flag = 0;
}
else
{
flag = 1;
}
}
}
}
本文介绍了一个使用QML创建的可滚动文本组件,该组件能够根据文本长度自动滚动显示,并支持重置文本位置的功能。通过调整参数可以轻松地改变文本的字体大小、颜色和滚动速度。
1012

被折叠的 条评论
为什么被折叠?



