Qt实现艺术字效果

  Qt实现艺术字效果,通常有三种方式,一种是通过绘制机制,另外一种是使用样式表,最后一种是通过图片代替,本次介绍使用绘制来实现艺术字效果。

    代码如下(分两种实现):

    第一种:

QPainter painter(this);
QPen pen;
pen.setWidth(2);
pen.setColor(Qt::red);

painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(pen);

QLinearGradient linearGrad;
bool ifTransparent = false;
if (ifTransparent){
linearGrad.setColorAt(0, Qt::transparent); // 字体透明 只有边框
} else {
linearGrad.setColorAt(0, Qt::blue); // 字体利用特定颜色来填充
}

QFont font;
font.setPointSize(40);
font.setBold(true);

QPainterPath textPath;
QRect painterRect = rect();
QString str = QStringLiteral("花莫弦");

textPath.addText(width() / 2, painterRect.bottom() / 2, font, str);
painter.setBrush(linearGrad);
painter.drawPath(textPath);
    效果图:        

    第二种:

QPainter painter(this);
QFont font;
font.setPointSize(40);
font.setBold(true);

QFontMetrics metrics(font);
QPainterPath path;
QPen pen(QColor(255, 0, 0, 100));
int penWidth = font.pointSize() * 0.5;
if (penWidth > 6) {
penWidth = 6;
}
pen.setWidth(penWidth);

int len = metrics.width(QStringLiteral("花莫弦"));
int w = width();
int px = (len - w) / 2;
if (px < 0) {
px = -px;
}

int py = (height() - metrics.height()) / 2 + metrics.ascent();
if(py < 0)
{
py = -py;
}
path.addText(px, py, font, QStringLiteral("花莫弦"));
painter.strokePath(path, pen);
painter.drawPath(path);
painter.fillPath(path, QBrush(Qt::blue));
    效果图:
---------------------
作者:花莫弦
来源:CSDN
原文:https://blog.csdn.net/u011822862/article/details/80559762
版权声明:本文为博主原创文章,转载请附上博文链接!

posted @ 2019-07-11 20:31  findumars  Views(1530)  Comments(0Edit  收藏  举报