Qt 文字描边(基础篇)

2023-12-14 21:02:20

项目中有时需要文字描边的功能

1.基础的绘制文字

使用drawtext处理

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
    painter.setRenderHints(QPainter::TextAntialiasing, true);

    QPen pen = painter.pen();

    QString m_textString = "文本God123";

    pen.setWidth(1);
    pen.setColor(QColor(255, 0, 0));
    painter.setPen(pen);
    painter.setBrush(QColor(255, 0, 0));
    //绘制白芯文字,注意文字的位置点设置
    painter.drawText(100,100,m_textString);

}

2.绘制黑边黄字

代码:

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
    painter.setRenderHints(QPainter::TextAntialiasing, true);

    QPen pen = painter.pen();

    QString m_textString = "文本God123";

    QPainterPath path;
    //注意文字的位置点设置
    path.addText(100,200,painter.font(), m_textString);

    pen.setWidth(2);
    pen.setColor(QColor(0, 0, 0));
    painter.setPen(pen);
    //
    painter.setBrush(QColor(255, 255, 0));
    //绘制黑边
    painter.drawPath(path);


}

3.只绘制黑边

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
    painter.setRenderHints(QPainter::TextAntialiasing, true);

    QPen pen = painter.pen();

    QString m_textString = "文本God123";

    QPainterPath path;
    //注意文字的位置点设置
    path.addText(100,200,painter.font(), m_textString);

    pen.setWidth(2);
    pen.setColor(QColor(0, 0, 0));
    painter.setPen(pen);
    //
    //painter.setBrush(QColor(255, 255, 0));
    //绘制黑边
    painter.drawPath(path);
}

4.绘制黑边红字(另一种方法)

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
    painter.setRenderHints(QPainter::TextAntialiasing, true);

    QPen pen = painter.pen();

    QString m_textString = "文本God123";

    QPainterPath path;
    //注意文字的位置点设置
    path.addText(100,200,painter.font(), m_textString);

    pen.setWidth(2);
    pen.setColor(QColor(0, 0, 0));
    painter.setPen(pen);
    //
    //painter.setBrush(QColor(255, 255, 0));
    //绘制黑边
    painter.drawPath(path);

    pen.setWidth(1);
    pen.setColor(QColor(255, 0, 0));
    painter.setPen(pen);
    painter.setBrush(QColor(255, 0, 0));
    //绘制白芯文字,注意文字的位置点设置
    painter.drawText(100,200,m_textString);
}

文章来源:https://blog.csdn.net/fengzhongye51460/article/details/135000724
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。