{"id":389,"date":"2020-04-06T10:48:46","date_gmt":"2020-04-06T16:48:46","guid":{"rendered":"http:\/\/www.eastfist.com\/qt_tutorials\/?p=389"},"modified":"2020-04-06T11:23:33","modified_gmt":"2020-04-06T17:23:33","slug":"disabling-antialiasing-when-drawing-fonts-in-qt-4-8-x","status":"publish","type":"post","link":"https:\/\/eastfist.com\/qt_tutorials\/disabling-antialiasing-when-drawing-fonts-in-qt-4-8-x\/","title":{"rendered":"Disabling antialiasing when drawing fonts in Qt 4.8.x"},"content":{"rendered":"<p>When you draw text in Qt using QPainter and desire it to be pixel crisp and sharp, you might do something like this:<\/p>\n<blockquote>\n<pre><code>QPainter painter(&this);\r\npainter.setRenderHint(QPainter::TextAntialiasing, false);\r\npainter.drawText(this.rect(), \"Some Text\");\r\n<\/code><\/pre>\n<\/blockquote>\n<figure id=\"attachment_397\" aria-describedby=\"caption-attachment-397\" style=\"width: 376px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.eastfist.com\/qt_tutorials\/wp-content\/uploads\/2020\/04\/antialiasTextOn.png\" alt=\"Qt 4.8.x QPainter drawing text with default anti-aliasing on\" width=\"376\" height=\"168\" class=\"size-full wp-image-397\" \/><figcaption id=\"caption-attachment-397\" class=\"wp-caption-text\">Qt 4.8.x QPainter drawing text with default anti-aliasing on<\/figcaption><\/figure>\n<p>However, sometimes it doesn&#8217;t work. Your text might be drawn with antialiasing no matter what, which results in soft blurry sometimes ugly font.<!--more--><\/p>\n<p>So try this:<\/p>\n<blockquote>\n<pre><code>QFont font;\r\nfont.setFamily(\"SomeFontFamily\");\r\nfont.setStyleStrategy(QFont::NoAntialias);\r\n\r\npainter.setFont(font);\r\n<\/code><\/pre>\n<\/blockquote>\n<figure id=\"attachment_400\" aria-describedby=\"caption-attachment-400\" style=\"width: 376px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.eastfist.com\/qt_tutorials\/wp-content\/uploads\/2020\/04\/antialiasTextOff.png\" alt=\"Qt 4.8.x QPainter drawing text with default anti-aliasing off, which requires finding the right function\" width=\"376\" height=\"168\" class=\"size-full wp-image-400\" \/><figcaption id=\"caption-attachment-400\" class=\"wp-caption-text\">Qt 4.8.x QPainter drawing text with default anti-aliasing off, which requires finding the right function<\/figcaption><\/figure>\n<p>Presto! Your font should be drawn all blocky and pixel retro style with no antialiasing. If that doesn&#8217;t work, you might want to pre-render your font characters as sprites and draw them as QImages or QPixmaps.<\/p>\n<p><strong>NOTE: If you&#8217;re using an Internet browser like Chrome, it might apply anti-aliasing on the reference images, so the image demonstrating anti-aliasing off won&#8217;t look correct. You&#8217;ll have to disable anti-aliasing in your Internet browser or download and view the image with an image viewer that will display it correctly. I attempted to disable antialiasing using CSS though.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you draw text in Qt using QPainter and desire it to be pixel crisp and sharp, you might do something like this: QPainter painter(&#038;this); painter.setRenderHint(QPainter::TextAntialiasing, false); painter.drawText(this.rect(), &#8220;Some Text&#8221;); However, sometimes it doesn&#8217;t work. Your text might be drawn with antialiasing no matter what, which results in soft blurry sometimes ugly font.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[127,132,128,135,131,130,133,3,134,129],"class_list":["post-389","post","type-post","status-publish","format-standard","hentry","category-qt-2","tag-4-8","tag-antialiasing","tag-blurry","tag-crisp","tag-disable","tag-font","tag-pixel","tag-qt","tag-sharp","tag-text"],"_links":{"self":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts\/389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/comments?post=389"}],"version-history":[{"count":18,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts\/389\/revisions"}],"predecessor-version":[{"id":409,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts\/389\/revisions\/409"}],"wp:attachment":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/media?parent=389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/categories?post=389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/tags?post=389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}