Qt QLabel + 定时器 实现滚动字幕

//首先在你的工程里要启动一个定时器(定时器的触发周期以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;//滚动的上一次时间
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值