Qt的卡拉OK歌词效果

先上图一张..



实现卡拉OK效果, 主要是使用了QPainter的叠加, 

[cpp] view plain copy
  1. pTmp.setCompositionMode(QPainter::CompositionMode_Source);  
  2. pTmp.fillRect(QRect(0, 0, paintw, rtText.height()), QBrush(Qt::white));  
  3. pTmp.fillRect(QRect(0, 0, mPrecent, rtText.height()), QBrush(Qt::blue));  
  4.   
  5. pTmp.setCompositionMode(QPainter::CompositionMode_DestinationIn);  
  6. pTmp.drawImage(0, 0, pmMask);  
  7. pTmp.setCompositionMode(QPainter::CompositionMode_DestinationOver);  


主要函数:

[cpp] view plain copy
  1. void testLyrics::DrawLyric(QPainter &p, const QRect& rt)  
  2. {  
  3.     p.setBrush(Qt::darkGray);  
  4.     p.drawRect(rect());  
  5.   
  6.     const QString strLyric = mCurLyrics.lyric;  
  7.     QFont m_ftLyric(QString::fromLocal8Bit("微软雅黑"), 24, QFont::Bold);  
  8.   
  9.     QPainter pTmp;  
  10.   
  11.     QFontMetrics fm(m_ftLyric);  
  12.     QSize textSize = fm.size(Qt::TextSingleLine, strLyric);  
  13.     QRect textRect(QPoint(0,0),textSize);  
  14.     int paintw = textSize.width();  
  15.     int playedW = textSize.width()*0.5;  
  16.   
  17.     QRect rtText = rect();  
  18.   
  19.     //  pTmp.setRenderHints(QPainter::TextAntialiasing, true);   
  20.     QImage pmMask(paintw, rtText.height(),QImage::Format_ARGB32);  
  21.     pmMask.fill(Qt::transparent);  
  22.     if (pTmp.begin(&pmMask))  
  23.     {  
  24.         pTmp.setFont(m_ftLyric);  
  25.         pTmp.setPen(Qt::black);  
  26.   
  27.         pTmp.drawText(pmMask.rect(), strLyric);  
  28.     }  
  29.     pTmp.end();  
  30.   
  31.     QImage pmFG(paintw, rtText.height(),QImage::Format_ARGB32);  
  32.     if (pTmp.begin(&pmFG))  
  33.     {  
  34.         pTmp.setCompositionMode(QPainter::CompositionMode_Source);  
  35.         pTmp.fillRect(QRect(0, 0, paintw, rtText.height()), QBrush(Qt::white));  
  36.         pTmp.fillRect(QRect(0, 0, mPrecent, rtText.height()), QBrush(Qt::blue));  
  37.   
  38.         pTmp.setCompositionMode(QPainter::CompositionMode_DestinationIn);  
  39.         pTmp.drawImage(0, 0, pmMask);  
  40.         pTmp.setCompositionMode(QPainter::CompositionMode_DestinationOver);  
  41.   
  42.         // shadow  
  43.         pTmp.setFont(m_ftLyric);  
  44.         pTmp.setPen(QColor(0,0,0,0x80));  
  45.   
  46.         pTmp.drawText(pmMask.rect(), strLyric);  
  47.     }  
  48.     pTmp.end();  
  49.   
  50.     p.drawImage(rtText.left(), rtText.top(), pmFG);  
  51. }  



实例下载:http://download.csdn.net/detail/robertkun/7297947


转自:http://blog.csdn.net/robertkun/article/details/25059463

你可能感兴趣的:(QT)