• 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
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    理解构造器
    if与switch的性能比较
    Java对象的内存(一)
    shell编程_条件判断if
    shell编程_基础&变量
    集群架构篇:Nginx架构演进<拆分数据库 多台web节点共享静态资源>
    集群架构篇:Nginx流行架构LNMP
    集群架构篇:Nginx常用模块
    LInux系统@安装CentOS7虚拟机
    docker pull越来越慢的解决方法
  • 原文地址:https://www.cnblogs.com/findumars/p/11172525.html
Copyright © 2020-2023  润新知