先上图一张..
实现卡拉OK效果, 主要是使用了QPainter的叠加,
- pTmp.setCompositionMode(QPainter::CompositionMode_Source);
- pTmp.fillRect(QRect(0, 0, paintw, rtText.height()), QBrush(Qt::white));
- pTmp.fillRect(QRect(0, 0, mPrecent, rtText.height()), QBrush(Qt::blue));
- pTmp.setCompositionMode(QPainter::CompositionMode_DestinationIn);
- pTmp.drawImage(0, 0, pmMask);
- pTmp.setCompositionMode(QPainter::CompositionMode_DestinationOver);
主要函数:
- void testLyrics::DrawLyric(QPainter &p, const QRect& rt)
- {
- p.setBrush(Qt::darkGray);
- p.drawRect(rect());
- const QString strLyric = mCurLyrics.lyric;
- QFont m_ftLyric(QString::fromLocal8Bit("微软雅黑"), 24, QFont::Bold);
- QPainter pTmp;
- QFontMetrics fm(m_ftLyric);
- QSize textSize = fm.size(Qt::TextSingleLine, strLyric);
- QRect textRect(QPoint(0,0),textSize);
- int paintw = textSize.width();
- int playedW = textSize.width()*0.5;
- QRect rtText = rect();
- // pTmp.setRenderHints(QPainter::TextAntialiasing, true);
- QImage pmMask(paintw, rtText.height(),QImage::Format_ARGB32);
- pmMask.fill(Qt::transparent);
- if (pTmp.begin(&pmMask))
- {
- pTmp.setFont(m_ftLyric);
- pTmp.setPen(Qt::black);
- pTmp.drawText(pmMask.rect(), strLyric);
- }
- pTmp.end();
- QImage pmFG(paintw, rtText.height(),QImage::Format_ARGB32);
- if (pTmp.begin(&pmFG))
- {
- pTmp.setCompositionMode(QPainter::CompositionMode_Source);
- pTmp.fillRect(QRect(0, 0, paintw, rtText.height()), QBrush(Qt::white));
- pTmp.fillRect(QRect(0, 0, mPrecent, rtText.height()), QBrush(Qt::blue));
- pTmp.setCompositionMode(QPainter::CompositionMode_DestinationIn);
- pTmp.drawImage(0, 0, pmMask);
- pTmp.setCompositionMode(QPainter::CompositionMode_DestinationOver);
- // shadow
- pTmp.setFont(m_ftLyric);
- pTmp.setPen(QColor(0,0,0,0x80));
- pTmp.drawText(pmMask.rect(), strLyric);
- }
- pTmp.end();
- p.drawImage(rtText.left(), rtText.top(), pmFG);
- }
实例下载:http://download.csdn.net/detail/robertkun/7297947
转自:http://blog.csdn.net/robertkun/article/details/25059463