简体   繁体   English

使用QPainter :: drawText()时文本没有抗锯齿吗?

[英]Text is not antialiased while using QPainter::drawText()?

While i'm trying to draw text using QPainter::drawText() the text is not antialiased (comparing to MS word) 当我尝试使用QPainter::drawText()绘制文本时,文本没有抗锯齿(与MS word相比)

void TextLabel::paintEvent(QPaintEvent*) {
    QPainter p(this);
    p.setRenderHint(QPainter::TextAntialiasing);

    QFont font;
    font.setFamily("Roboto medium");
    font.setPointSize(32);
    font.setStyleHint(QFont::Helvetica, QFont::PreferAntialias);

    p.setPen(_brush);
    p.setFont(font);

    p.drawText(rect(), Qt::AlignLeft , _text);
}

Qt Doc says: Qt Doc说:

QPainter::TextAntialiasing -> Indicates that the engine should antialias text if possible QPainter :: TextAntialiasing->表示引擎应尽可能对文本进行抗锯齿

Is this impossible ? 这不可能吗? What should i do ? 我该怎么办 ?

The word one: 一句话:

在此处输入图片说明

The Qt one : Qt之一:

在此处输入图片说明

Try painting via a QImage -- the QPainter::TextAntialiasing is more likely to be honoured that way. 尝试通过QImage绘画QPainter::TextAntialiasing更可能以这种方式获得荣誉。

QImage image(size(), QImage::Format_ARGB32_Premultiplied);
{
  QPainter p(&image);
  p.setRenderHint(QPainter::TextAntialiasing);

  QFont font;
  font.setFamily("Roboto medium");
  font.setPointSize(16);
  font.setStyleHint(QFont::Helvetica, QFont::PreferAntialias);

  p.setPen(_brush);
  p.setFont(_font);

  p.drawText(rect(), Qt::AlignLeft , _text);
}
QPainter p(this);
p.drawImage(rect(), image);

Note: if this works then the QImage used should probably be a private class member rather than recreating it every time paintEvent is invoked. 注意:如果可行,则使用的QImage可能应该是私有类成员,而不是每次调用paintEvent时都重新创建它。

Seems it's an issue Qt has on Window OS (font rendering) and work with some fonts >=48pt and doesn't work with some other. 看来这是Qt在Window OS(字体渲染)上遇到的一个问题,并且不能用于某些> = 48pt的字体,而不能用于其他某些字体。

Issue : https://bugreports.qt.io/browse/QTBUG-40052 问题: https : //bugreports.qt.io/browse/QTBUG-40052

We hope they will fix it in the near future. 我们希望他们会在不久的将来修复它。

You can draw with QPainterPath it's more expensive but still helps : 您可以使用QPainterPath进行绘制,它虽然更昂贵,但仍然有帮助:

void TextLabel::paintEvent(QPaintEvent*) {
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(Qt::black);

    QFont font;
    font.setPointSize(38);
    font.setFamily("Roboto");

    painter.setFont(font);

    painter.drawText(0, 60, "Google");

    QPainterPath textPath;
    textPath.addText(0, 140, font, "Google");
    painter.drawPath(textPath);
}

Roboto @ 38pt : Roboto @ 38pt:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM