Using Painter correctly in order to draw text.
-
Hi everyone,
I was learning how to draw various things in Qt using painter when I arrived at the subject of text.
What I did was something like this:
@painter.save();
QRect rec(0, 0, 20, 30);
painter.setPen(Qt::black);
//painter.drawText(rec, Qt::AlignLeft, "am");QTextDocument doc;
doc.setTextWidth(rec.width());doc.setHtml("<b>am</b>");
doc.drawContents(&painter, rec);painter.restore();@
So as you can see I tried using "drawText" and QTextDocument.
I noticed though that in my Mac the resulting seems better (by that I mean more crisp or antialised), while trying it on Windows the text seems more pixelated even though I set every flag I know
@painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHints(QPainter::HighQualityAntialiasing);
painter.setRenderHints(QPainter::SmoothPixmapTransform);
painter.setRenderHints(QPainter::TextAntialiasing);@Why is this happening?
Also I could not find a way to alter the size of the text. Can someone tell me (or give me a link) a simple template on drawing and customising text using Painter?
-
I think this is related to several problems on windows.
https://bugreports.qt.io/browse/QTBUG-44437
--> QGraphicsTextItem is using an QTextDocument internalhttps://bugreports.qt.io/browse/QTBUG-20900
Is there any reason you using QTextDocument.
For simple Text you could for example draw it like :
@QFont font("Arial",12);
font.setBold(true);
font.setItalic(true);
painter.setFont(font);QTextOption opt;
opt.setAlignment(Qt::AlignCenter);painter.drawText(QRectF(0,0,50,50),"This is yout text",opt);@
-
When you want the same look on windows and mac you can try to use setPixelSize on QFont :
http://doc-snapshot.qt-project.org/qt5-5.4/qfont.html#setPixelSizeThat will ensure that the font has the same size on different platforms.
When nothing helps you could also try to generate a QPainterPath.