QLabel 文字水平滚动显示

先上效果图:

看源码:

1. 自适应函数,判断label文本是否需要滚动起来。

void MLabel::upateLabelRollingState()
{
    //  获取文本大小,小于文本框长度,则无需滚动
    QFont ft = font();
    ft.setPointSize(fontSize);

    QFontMetrics fm(ft);
    int nW = fm.width(text());

    left = 0;
    //  开启文本框滚动
    if(nW > width())
    {
        timerId = startTimer(100);
    }
    //  关闭文本框滚动
    else
    {
        if(timerId >= 0)
        {
            killTimer(timerId);
            timerId = -1;
        }
    }
}

 2. 定时移动文本位置

void MLabel::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == timerId && isVisible())
    {
        //  每次左移1个像素
        left += 1;

        //  判断是否已经完成一遍循环,完成则恢复起始位置,重新开始循环
        QFont ft = font();
        ft.setPointSize(fontSize);
        QFontMetrics fm(ft);
        int txtWidth = fm.width(text());
        int spaceWidth = fm.width(strSpace);

        if((txtWidth + spaceWidth) < left)
            left = 0;

        repaint();
    }

    QLabel::timerEvent(e);
}

3. 重绘事件,动态显示文本

void MLabel::paintEvent(QPaintEvent *e)
{
    QPainter p(this);

    //  获取文本框的大小
    QRect rc = rect();
    rc.setHeight(rc.height() - 2);
    rc.setWidth(rc.width() - 2);

    //  设置即将绘制文字的字体
    QFont ft = font();
    ft.setPointSize(fontSize);
    p.setFont(ft);
    p.setPen(QPen(Qt::red));

    //  设置绘制文字的开始位置,也就是将文字往左移动多少
    rc.setLeft(rc.left() - left);

    //  如果文字已经显示到末尾,则再添加一遍文字,做出循环滚动的效果
    QString strText = text();
    if(timerId >= 0)
        strText += strSpace + text();

    //  绘制文字
    p.drawText(rc, Qt::AlignVCenter, strText);
}

4. 使用效果,在设置文本、缩放事件两次调用自适应的函数。

void MLabel::setText(const QString & txt)
{
    QLabel::setText(txt);

    upateLabelRollingState();
}

void MLabel::resizeEvent(QResizeEvent *e)
{
    QLabel::resizeEvent(e);

    upateLabelRollingState();
}

完成,使用时,将QLabel提升为MLabel即可。

最后附上源码下载地址:https://download.csdn.net/download/chenxipu123/10966082

吐槽一下:CSDN不让设置资源所需的下载积分,所以源码没法免费共享。

你可能感兴趣的:(qt)