zoukankan      html  css  js  c++  java
  • QT显示歌词渐变

           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函数

      

  • 相关阅读:
    程序开发
    主方法
    日志
    node.js
    二维互换
    前台打断点
    具体的后台断点快捷键
    Jenkins
    断点
    循环
  • 原文地址:https://www.cnblogs.com/xshang/p/5633770.html
Copyright © 2011-2022 走看看