//首先在你的工程里要启动一个定时器(定时器的触发周期以100ms间隔为宜
void QWidgetProperty::timerEvent(QTimerEvent *evt)
{
if (pageCaption->isVisible())//为了节省计算资源,确保你的界面可视情况下才滚动字幕
{
//记录一下最后一次滚动时间,用来控制滚动速度
static QDateTime dtLast = QDateTime::currentDateTime();
QDateTime dtNow = QDateTime::currentDateTime();
int delt = 100;
switch (prevset.sys_marquee)
{
case 0: //不滚动
return;
case 1://速度1
case 2://速度2
case 3://速度3
case 4://速度4
case 5://速度5
delt = 100 * (6 - prevset.sys_marquee);//间隔越小速度越快
break;
default://默认速度
delt = 400;
break;
}
if (dtLast.msecsTo(dtNow)>delt)
{
static int idx = 0;
static int DELTA = 0;
static QString strSpace = "";
QFontMetrics fm = labelPreview->fontMetrics();
int w = fm.width(prevset.sys_caption);
int delt = labelPreview->width() - w;
if (DELTA == 0 || DELTA != delt)
{
DELTA = delt;
strSpace = "";
while (fm.width(strSpace) < DELTA)
{
strSpace.append(" ");
}
}
QString strNew = prevset.sys_caption + strSpace;
QString strHead = strNew.mid(0, idx);
QString strTail = strNew.mid(idx, strNew.length() - idx);
if (idx < strNew.length())idx++;
else idx = 0;
strTail.append(strHead);
labelPreview->setText(strTail);
dtLast = dtNow;//滚动的上一次时间
}
}
}