窗体部件效果之滚动字幕

来源:互联网 发布:r2v32软件 编辑:程序博客网 时间:2024/06/06 04:56

scrollcaption.cpp

#include "widget.h"#include "ui_widget.h"#pragma execution_character_set("utf-8")Widget::Widget(QWidget *parent) :    QWidget(parent),    ui(new Ui::Widget){    ui->setupUi(this);    m_scrollCaptionStr = QStringLiteral("欢迎加入我们:Qt分享&&交流 26197884");    m_timer = new QTimer(this);    connect(m_timer,  SIGNAL(timeout()),  this,  SLOT(scrollCaption()));    m_timer->start(50);}Widget::~Widget(){    delete ui;}void Widget::scrollCaption(){    //mothod 1(not sequential)    static int nPos = 0;    if (nPos > m_scrollCaptionStr.length())    {        nPos = 0;    }    ui->m_scrollCaptionLabel->setText(m_scrollCaptionStr.mid(nPos));    nPos++;    //mothod 2(not universal)    ui->m_scrollCaptionLabel_2->setText(m_scrollCaptionStr);    if(ui->m_scrollCaptionLabel_2->pos().x() + ui->m_scrollCaptionLabel_2->width() < 0)    {        ui->m_scrollCaptionLabel_2->move(width(), ui->m_scrollCaptionLabel_2->pos().y());    }    ui->m_scrollCaptionLabel_2->move(ui->m_scrollCaptionLabel_2->pos().x()-1, ui->m_scrollCaptionLabel_2->pos().y());}