{"id":118,"date":"2017-03-03T14:43:07","date_gmt":"2017-03-03T14:43:07","guid":{"rendered":"http:\/\/privateblog.nobytesleft.net\/?p=118"},"modified":"2017-03-03T15:13:14","modified_gmt":"2017-03-03T15:13:14","slug":"qt-image-size-in-push-buttons-and-labels","status":"publish","type":"post","link":"https:\/\/privateblog.nobytesleft.net\/?p=118","title":{"rendered":"Qt image size in push buttons and labels"},"content":{"rendered":"<p>Often we want to show an image in a push button (QPushButton) or in a window (using QLabel). The automatic resizing offered by the Qt framework isn&#8217;t always working, so I use the following trick.<\/p>\n<p>The following global functions are defined:<\/p>\n<pre class=\"lang:c++ decode:true\">#include &lt;QApplication&gt;\r\n\r\ndouble getLogicalDPI(void)\r\n{\r\n    return QApplication::screens().at(0)-&gt;logicalDotsPerInch();\r\n}\r\n\r\ndouble get_resolution_ratio_vs_mdpi(void)\r\n{\r\n    const double mdpi_DPI=80.; \/\/ really this is a \"logical\" DPI resolution - accounting for some scaling by Qt, the OS and so on. Use this.\r\n    static double resolution_ratio=\r\n        getLogicalDPI()\/mdpi_DPI;\r\n    return resolution_ratio;\r\n}\r\n<\/pre>\n<p>In\u00a0 any form constructor, we do the following:<\/p>\n<pre class=\"lang:c++ decode:true\">{\r\n\u00a0\u00a0\u00a0 ...\r\n    ui-&gt;setupUi(this);\r\n\u00a0\u00a0\u00a0 ...\r\n\u00a0\u00a0\u00a0 \/\/ Fix icon sizes for high-DPI displays\r\n\u00a0\u00a0\u00a0 double scale_factor=get_resolution_ratio_vs_mdpi();\r\n\u00a0\u00a0\u00a0 \/\/ for icons in push buttons:\r\n\u00a0\u00a0\u00a0 ui-&gt;acceptButton-&gt;setIconSize(ui-&gt;acceptButton-&gt;iconSize()*scale_factor);\r\n\u00a0\u00a0\u00a0 \/\/ for icons in labels:\r\n\u00a0\u00a0\u00a0 ui-&gt;label_chat_icon-&gt;setFixedSize(ui-&gt;label_chat_icon-&gt;minimumSize()*scale_factor);\r\n\u00a0\u00a0\u00a0 ...\r\n}\r\n<\/pre>\n<p>In any .ui file, we do the following. For a push button, we set the iconSize property to the size we would like at mdpi (normal resolution). For a QLabel, we set sizePolicy to Fixed, then minimumSize = maximumSize = the size we would like at mdpi (normal resolution).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Often we want to show an image in a push button (QPushButton) or in a window (using QLabel). The automatic resizing offered by the Qt framework isn&#8217;t always working, so&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-118","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=118"}],"version-history":[{"count":3,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts\/118\/revisions"}],"predecessor-version":[{"id":121,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=\/wp\/v2\/posts\/118\/revisions\/121"}],"wp:attachment":[{"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/privateblog.nobytesleft.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}