IMAN4K
IMAN4K

Reputation: 1345

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)

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:

QPainter::TextAntialiasing -> Indicates that the engine should antialias text if possible

Is this impossible ? What should i do ?

The word one:

enter image description here

The Qt one :

enter image description here

Upvotes: 3

Views: 3367

Answers (2)

IMAN4K
IMAN4K

Reputation: 1345

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.

Issue : 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 :

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 :

enter image description here

Upvotes: 3

G.M.
G.M.

Reputation: 12879

Try painting via a QImage -- the QPainter::TextAntialiasing is more likely to be honoured that way.

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.

Upvotes: 0

Related Questions