{"id":399,"date":"2018-02-02T14:26:48","date_gmt":"2018-02-02T13:26:48","guid":{"rendered":"https:\/\/tollana.d-tor.org\/notes-to-self\/?p=399"},"modified":"2018-02-02T14:26:48","modified_gmt":"2018-02-02T13:26:48","slug":"how-to-create-a-qicon-with-qt-programmatically","status":"publish","type":"post","link":"https:\/\/tollana.d-tor.org\/notes-to-self\/?p=399","title":{"rendered":"How to create a QIcon with Qt programmatically"},"content":{"rendered":"<p>First, create a 32&#215;32 QImage and make it transparent (or whatever background color you need):<\/p>\n<pre>QImage img(32, 32, QImage::Format_ARGB32);\r\nimg.fill(QColor(0, 0, 0, 0));<\/pre>\n<p>I use QImage instead of QPixmap, because the <a href=\"http:\/\/doc.qt.io\/qt-5\/qimage.html#details\">documentation<\/a> says that QImage is optimized for I\/O, e.g. painting, and the first attempts with QPixmap looked like crap. Next, create a QPainter for the QPaintDevice and set some render hints to make it look nice:<\/p>\n<pre class=\"add\">QPainter *p = new QPainter(&amp;img);\r\np-&gt;setRenderHint(QPainter::Antialiasing);\r\np-&gt;setRenderHint(QPainter::TextAntialiasing);\r\np-&gt;setRenderHint(QPainter::SmoothPixmapTransform);<\/pre>\n<p>QPainter::setBrush() sets the background color for shapes, QPainter::setPen() the foreground color for text:<\/p>\n<pre>p-&gt;setBrush(QColor(Qt::red));\r\np-&gt;setPen(QColor(Qt::white));<\/pre>\n<p>Then select a font the size of our future Icon:<\/p>\n<pre>QFont f(\"courier new\");\r\nf.setPixelSize(32);\r\np-&gt;setFont(f);<\/pre>\n<p>Now we need some background. White on transparent isn&#8217;t really readable, so let&#8217;s draw a circle:<\/p>\n<pre class=\"add\">p-&gt;drawEllipse(img.rect());<\/pre>\n<p>Since our QImage is actually a square and not an rectangle, QPainter::drawEllipse will draw a circle. Print the QChar, letter, or whatever:<\/p>\n<pre>p-&gt;drawText(img.rect(), Qt::AlignCenter, QChar(char));<\/pre>\n<p>Now clean up and return the QImage as QIcon:<\/p>\n<pre>delete p;\r\nreturn QIcon(QPixmap::fromImage(img));<\/pre>\n<p>The whole shebang can be marveled at <a href=\"https:\/\/tollana.d-tor.org\/cgit\/ShemovCleaner\/commit\/?id=fd217e0513d450fc78345d6b040eda7893d815a8\">here<\/a>. It looks like this:<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/tollana.d-tor.org\/notes-to-self\/wp-content\/uploads\/2018\/02\/programicon.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-401\" src=\"https:\/\/tollana.d-tor.org\/notes-to-self\/wp-content\/uploads\/2018\/02\/programicon.png\" alt=\"\" width=\"80\" height=\"39\" \/><\/a><\/p>\n<p>Have fun!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First, create a 32&#215;32 QImage and make it transparent (or whatever background color you need): QImage img(32, 32, QImage::Format_ARGB32); img.fill(QColor(0, 0, 0, 0)); I use QImage instead of QPixmap, because the documentation says that QImage is optimized for I\/O, e.g. painting, and the first attempts with QPixmap looked like crap. Next, create a QPainter for &hellip; <a href=\"https:\/\/tollana.d-tor.org\/notes-to-self\/?p=399\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to create a QIcon with Qt programmatically<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77,43],"tags":[96,113],"class_list":["post-399","post","type-post","status-publish","format-standard","hentry","category-linux","category-windows","tag-programming","tag-qt"],"_links":{"self":[{"href":"https:\/\/tollana.d-tor.org\/notes-to-self\/index.php?rest_route=\/wp\/v2\/posts\/399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tollana.d-tor.org\/notes-to-self\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tollana.d-tor.org\/notes-to-self\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tollana.d-tor.org\/notes-to-self\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tollana.d-tor.org\/notes-to-self\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=399"}],"version-history":[{"count":2,"href":"https:\/\/tollana.d-tor.org\/notes-to-self\/index.php?rest_route=\/wp\/v2\/posts\/399\/revisions"}],"predecessor-version":[{"id":402,"href":"https:\/\/tollana.d-tor.org\/notes-to-self\/index.php?rest_route=\/wp\/v2\/posts\/399\/revisions\/402"}],"wp:attachment":[{"href":"https:\/\/tollana.d-tor.org\/notes-to-self\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tollana.d-tor.org\/notes-to-self\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tollana.d-tor.org\/notes-to-self\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}