码迷,mamicode.com
首页 > 其他好文 > 详细

QT显示歌词渐变

时间:2016-07-01 18:01:34      阅读:397      评论:0      收藏:0      [点我收藏+]

标签:

       central = new QWidget(this);
	setCentralWidget(central);
	central->setAutoFillBackground(true);
	central->setWindowOpacity(1);
	label = new QLabel(tr("这是一首歌,请你记住它,不要忘记"),central);
	label2 = new QLabel(central);
	label2->move(100, 150);
	label2->resize(500,50);
	label->move(100, 150);
	label->resize(500,50);
	QFont ft;
	ft.setPointSize(15);
	label->setFont(ft);
	label->setAttribute(Qt::WA_TranslucentBackground);
	label2->setFont(ft);
	label2->setAttribute(Qt::WA_TranslucentBackground);
	str = tr("这是一首歌,请你记住它,不要忘记");
	count=1;
	QTimer *timer = new QTimer(this);
	connect(timer, SIGNAL(timeout()), this, SLOT(paint()));
	timer->start(1000);
void label_test::paint()
{
	count++;
	update();
	qDebug()<<count;
}

void label_test::paintEvent(QPaintEvent *e)
{
	QLinearGradient linearGrad(QPointF(100, 150), QPointF(200, 200));
	linearGrad.setColorAt(0, Qt::red);
	linearGrad.setColorAt(0.5, Qt::yellow);
	linearGrad.setColorAt(1, Qt::green);

	QBrush brush(linearGrad);
	QPalette palette;
	palette.setBrush(QPalette::WindowText, brush);
	
	int length = str.length();
	if(count > length)
	{
		count = 1;
	}
	label2->setPalette(palette);
	//label->setText("");
	QString s = str.mid(0, count);

	label2->setText(s);
	label2->show();
	
	
}

主要思路是用两个qlabel 控件,第二个QLabel控件每次都重新绘制第一个QLabel.

repaint会自动调用paintEvent函数

  

QT显示歌词渐变

标签:

原文地址:http://www.cnblogs.com/xshang/p/5633770.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!