简述
滚动字幕,也就是传说中的跑马灯效果。
简单地理解就是:每隔一段时间(一般几百毫秒效果较佳)显示的文字进行变化(即滚动效果)。
实现
利用定时器QTimer,在固定的时间(这里为200毫秒)截取文本,来实现滚动效果!
效果
源码
首先,我们需要定义显示的滚动字幕:
const QString strScrollCation = QString::fromLocal8Bit"一去丶二三里 - 青春不老,奋斗不止!");
定义QLabel进行文本的显示,利用QTimer定时更新。
m_pLabel = new QLabelthis);
QTimer *pTimer = new QTimerthis);
connectpTimer, SIGNALtimeout)), this, SLOTscrollCaption)));
// 定时200毫秒
pTimer->start200);
实现槽函数,进行滚动更新:
void MainWindow::scrollCaption)
{
static int nPos = 0;
// 当截取的位置比字符串长时,从头开始
if nPos > strScrollCation.length))
nPos = 0;
m_pLabel->setTextstrScrollCation.midnPos));
nPos++;
}