Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-44486

QIcon has problem handling high DPI pixmaps

XMLWordPrintable

      When a QIcon is supplied with a QPixmap with devicePixelRatio higher than 1, the icon is not painted at the right size at all time.

      Build and run the following code:

      #include <QtWidgets>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QPixmap iconPix(50, 50);
          iconPix.fill(Qt::transparent);
          QPainter painter(&iconPix);
          painter.setBrush(Qt::red);
          painter.setPen(Qt::transparent);
          painter.setRenderHint(QPainter::Antialiasing);
          painter.drawEllipse(iconPix.rect());
          iconPix.setDevicePixelRatio(2);
      
          QIcon icon;
          iconPix.setDevicePixelRatio(2);
          icon.addPixmap(iconPix);
      
          QWidget w;
          auto layout = new QVBoxLayout;
          w.setLayout(layout);
      
          layout->addWidget(new QLabel("Icon pixmap: 50x50@2X"));
      
          auto btn = new QToolButton;
          btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
          btn->setIcon(icon);
          btn->setIconSize(QSize(64,64));
          btn->setText("Icon size: 64x64");
          layout->addWidget(btn);
      
          btn = new QToolButton;
          btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
          btn->setIcon(icon);
          btn->setIconSize(QSize(32,32));
          btn->setText("Icon size: 32x32");
          layout->addWidget(btn);
      
          w.show();
      
          return a.exec();
      }
      

      The first tool button has the icon sized correctly when the requested icon size (64x64) is larger than the pixmap pixel size (50x50). But the layout in the button is really messed up.

      The second tool button with the same icon has the icon filling up the whole requested size (32X32), which is still larger than the supposed layout size of the icon (25x25).

      It seems that the icon ignores the devicePixelRatio when the requested size is smaller than the pixmap pixel size.

      See the attached screenshot for reference.

        1. pixratio.PNG
          4 kB
          Stephen Chu
        2. more sizes.PNG
          12 kB
          Stephen Chu
        3. pixmap_54_55_56.png
          12 kB
          Alessandro Portale
        4. qtbug44486.zip
          3 kB
          Friedemann Kleint
        For Gerrit Dashboard: QTBUG-44486
        # Subject Branch Project Status CR V

            sorvig Morten Sørvig
            stephenju Stephen Chu
            Votes:
            12 Vote for this issue
            Watchers:
            26 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes