Qt QLabel左跑马显示文字

开发中可能会需要加一些跑马的效果,会使界面看起来很炫,而且可以达到提醒用户的作用。

简单实现了左跑马的效果,使用的是QLabel显示的文字,利用QTimer来控制跑马的速度:

效果如图:


代码如下:

#ifndef TEXTTICKER_H
#define TEXTTICKER_H

#include <QtWidgets/QWidget>
#include <QtWidgets/QLabel>

class TextTicker : public QWidget
{
	Q_OBJECT

public:
	TextTicker(QWidget *parent = 0);
	~TextTicker();

private:
	void showTicker();

	int m_curIndex;
	QLabel *m_label;
	QString m_showText;
};

#endif // TEXTTICKER_H
#include "textticker.h"
#include <QTimer>

TextTicker::TextTicker(QWidget *parent)
	: QWidget(parent)
{
	m_curIndex = 0;//当前角码
	m_showText = "This is a textTicker Text!";//显示的文字
	m_label = new QLabel(this);
	m_label->setMinimumWidth(300);
	QTimer *timer = new QTimer(this);
	connect(timer, &QTimer::timeout, this, &TextTicker::showTicker);
	timer->start(100);//定时器
}

TextTicker::~TextTicker()
{

}

void TextTicker::showTicker()
{
	int iSize = m_showText.size();
	if (m_curIndex >= iSize)
		m_curIndex = 0;
	m_label->setText(m_showText.mid(m_curIndex++));
	//m_label->setText(m_showText.right(iSize - m_curIndex++));
}

交流Q:1245178753

本文地址:http://blog.csdn.net/u011417605/article/details/51207274

源码下载:http://download.csdn.net/detail/u011417605/9497827

你可能感兴趣的:(qt,QLabel,跑马)