Reputation: 1345
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:
The Qt one :
Upvotes: 3
Views: 3367
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 :
Upvotes: 3
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