Text from QPainter is much nicer than QPainterPath

I want to draw text using QPainter , and first I want to use QPainterPath (because in the end I want to rotate the text in different ways). However, I find that the text created by QPainterPath is much uglier than the text created by QPainter .

The following code:

 void MyWidget::paintEvent(QPaintEvent* /*event*/) { QFont font; font.setStyleHint(QFont::Times, QFont::PreferAntialias); font.setPointSize(30); QPainter painter; painter.begin(this); painter.setRenderHint(QPainter::Antialiasing); painter.setBrush(Qt::black); painter.setFont(font); painter.drawText(10, 40, "Hello World"); QPainterPath textPath; textPath.addText(10, 100, font, "Hello world"); painter.drawPath(textPath); painter.end(); } 

gives the following result:

enter image description here

The first is clearly much cleaner and nicer, especially in small fonts. What should I do to get the same result from QPainterPath ?

I am doing the above results on a computer running Windows 7 with Qt 5.0.

+6
source share
2 answers

According to the Qt documentation for adding text to QPainterPath: -

Adds this text to this path as a set of private subfolders created from the supplied font.

So, there is a conversion, so it does not look the same. If you need to rotate the text, you can try to rotate the QPainter before rendering and then restore it later. Alternatively, if you can use QGraphicsView and QGraphicsDisplay instead of just displaying on the widget, there is a QGraphicsTextItem class that can help.

But in general, this is a transformation into a set of closed subfolders that are responsible for the different output of text quality.

+5
source

The two fonts do not look the same because you add extra outlines to the QPainterPath text. The following code snippet should give good results:

 QFont font; font.setStyleHint(QFont::Times, QFont::PreferAntialias); font.setPointSize(30); QPainter painter; painter.begin(this); painter.setRenderHints(QPainter::TextAntialiasing | QPainter::Antialiasing); painter.setFont(font); // painter text color is modified by setPen() painter.setPen(Qt::white); painter.drawText(10, 40, "Hello World 1"); QPainterPath textPath; textPath.addText(10, 100, font, "Hello World 2"); // painter path text color is modified by setBrush() painter.setBrush(Qt::white); // setPen(Qt::white) add extra white contour on text path (what you did) painter.setPen(Qt::white); painter.drawPath(textPath); QPainterPath textPath2; textPath2.addText(10, 160, font, "Hello World 3"); // painter path text color is modified by setBrush painter.setBrush(Qt::white); // setPen(Qt::NoPen) avoid extra contours for QPainter Path painter.setPen(Qt::NoPen); painter.drawPath(textPath2); painter.end(); 

I admit that the QPainterPath "Hello World 3" text is a bit uglier than the QPainterPext "Hello World 1", but the result is still better than "Hello World 2"

enter image description here

+1
source

Source: https://habr.com/ru/post/950179/


All Articles