Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with DPI and QIcon

Problem with DPI and QIcon

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 471 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kattia
    wrote on last edited by Kattia
    #1

    I'm using Qt 6.5 on Windows 10, the pixmaps on my application are scaled differently according to the current DPI.

    A minimal reproducible exxample:

    Windows scale 100%
    del_YcpVZze4Xo.png

    scale 125%
    del_KFwUNzwjVc.png

    scale 150%
    del_KkC7k6lJzQ.png

    class Label : public QLabel
    {
        Q_OBJECT
    public:
        QIcon icon;
        Label(QWidget* parent = nullptr) : QLabel(parent){}
        void paintEvent(QPaintEvent* event) override
        {
    	QPainter painter(this);
    	painter.setRenderHint(QPainter::Antialiasing);
    	painter.setRenderHint(QPainter::SmoothPixmapTransform);
            icon.paint(&painter, rect(), Qt::AlignCenter, QIcon::Normal, QIcon::On);
        }
    };
    
    inline QPixmap setRoundness(QPixmap pixmap, int roundness)
    {
    	QPainterPath path;
            qDebug() << pixmap.width() << pixmap.height();
    	// Round the pixmap by 50%
            path.addRoundedRect(QRectF(0, 0, pixmap.width(), pixmap.height()), pixmap.width() / 2, pixmap.height() / 2);
    
    	QPixmap roundedPixmap = QPixmap(pixmap.size());
    	roundedPixmap.fill(Qt::transparent);
    	QPainter painter(&roundedPixmap);
    	painter.setRenderHint(QPainter::Antialiasing, true);
    	painter.setRenderHint(QPainter::SmoothPixmapTransform);
    	painter.fillPath(path, Qt::black);
    	painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
    	painter.drawPixmap(0, 0, pixmap);
    
            pixmap.save("test_1.png");
            roundedPixmap.save("test_2.png");
    	return roundedPixmap;
    }
    
    class MainWindow : public QMainWindow
    {
       Q_OBJECT
    public:
    MainWindow(QWidget *parent = nullptr) : QMainWindow(parent), ui(new Ui::MainWindowClass())
    {
          ui->setupUi(this);
          setStyleSheet("#centralWidget { background-color: rgba(80, 80, 80, 80); }");
    
          QIcon icon = QIcon("C:/Users/Katia/test.png"); // test.png => https://i.imgur.com/cZfWHkW.png
          QPixmap roundedPixmap = setRoundness(icon.pixmap(QSize(55, 55)), 50);
    
          Label* label = new Label;
          label->icon = roundedPixmap;
          QLabel* label_2 = new QLabel;
          label_2->setPixmap(roundedPixmap);
    
          QWidget* widget = new QWidget;
          QHBoxLayout* hLayout = new QHBoxLayout(widget);
          hLayout->addWidget(label);
          hLayout->addWidget(label_2);
          hLayout->setContentsMargins(0, 0, 0, 0);
          hLayout->setSpacing(30);
    
          setCentralWidget(widget);
          setContentsMargins(32, 32, 32, 32);
          return;
       }
    }
    

    Its a problem in the QPixmap and not in QPaintEvent because when i save roundedPixmap.save("test_2.png"); this is what i get:
    (DPI 150%)
    x2.png

    (DPI100%)
    x2.png

    How i could fix this dpi problem?

    Christian EhrlicherC 1 Reply Last reply
    0
    • K Kattia

      I'm using Qt 6.5 on Windows 10, the pixmaps on my application are scaled differently according to the current DPI.

      A minimal reproducible exxample:

      Windows scale 100%
      del_YcpVZze4Xo.png

      scale 125%
      del_KFwUNzwjVc.png

      scale 150%
      del_KkC7k6lJzQ.png

      class Label : public QLabel
      {
          Q_OBJECT
      public:
          QIcon icon;
          Label(QWidget* parent = nullptr) : QLabel(parent){}
          void paintEvent(QPaintEvent* event) override
          {
      	QPainter painter(this);
      	painter.setRenderHint(QPainter::Antialiasing);
      	painter.setRenderHint(QPainter::SmoothPixmapTransform);
              icon.paint(&painter, rect(), Qt::AlignCenter, QIcon::Normal, QIcon::On);
          }
      };
      
      inline QPixmap setRoundness(QPixmap pixmap, int roundness)
      {
      	QPainterPath path;
              qDebug() << pixmap.width() << pixmap.height();
      	// Round the pixmap by 50%
              path.addRoundedRect(QRectF(0, 0, pixmap.width(), pixmap.height()), pixmap.width() / 2, pixmap.height() / 2);
      
      	QPixmap roundedPixmap = QPixmap(pixmap.size());
      	roundedPixmap.fill(Qt::transparent);
      	QPainter painter(&roundedPixmap);
      	painter.setRenderHint(QPainter::Antialiasing, true);
      	painter.setRenderHint(QPainter::SmoothPixmapTransform);
      	painter.fillPath(path, Qt::black);
      	painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
      	painter.drawPixmap(0, 0, pixmap);
      
              pixmap.save("test_1.png");
              roundedPixmap.save("test_2.png");
      	return roundedPixmap;
      }
      
      class MainWindow : public QMainWindow
      {
         Q_OBJECT
      public:
      MainWindow(QWidget *parent = nullptr) : QMainWindow(parent), ui(new Ui::MainWindowClass())
      {
            ui->setupUi(this);
            setStyleSheet("#centralWidget { background-color: rgba(80, 80, 80, 80); }");
      
            QIcon icon = QIcon("C:/Users/Katia/test.png"); // test.png => https://i.imgur.com/cZfWHkW.png
            QPixmap roundedPixmap = setRoundness(icon.pixmap(QSize(55, 55)), 50);
      
            Label* label = new Label;
            label->icon = roundedPixmap;
            QLabel* label_2 = new QLabel;
            label_2->setPixmap(roundedPixmap);
      
            QWidget* widget = new QWidget;
            QHBoxLayout* hLayout = new QHBoxLayout(widget);
            hLayout->addWidget(label);
            hLayout->addWidget(label_2);
            hLayout->setContentsMargins(0, 0, 0, 0);
            hLayout->setSpacing(30);
      
            setCentralWidget(widget);
            setContentsMargins(32, 32, 32, 32);
            return;
         }
      }
      

      Its a problem in the QPixmap and not in QPaintEvent because when i save roundedPixmap.save("test_2.png"); this is what i get:
      (DPI 150%)
      x2.png

      (DPI100%)
      x2.png

      How i could fix this dpi problem?

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      Multiply your rect() with the devicePixelRatio() of your widget: https://doc.qt.io/qt-6/qpainter.html#drawing-high-resolution-versions-of-pixmaps-and-images

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      K 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        Multiply your rect() with the devicePixelRatio() of your widget: https://doc.qt.io/qt-6/qpainter.html#drawing-high-resolution-versions-of-pixmaps-and-images

        K Offline
        K Offline
        Kattia
        wrote on last edited by
        #3

        @Christian-Ehrlicher what rect the QPainter path rect? and how do you multiply a rect? only its width/height?

        Christian EhrlicherC 1 Reply Last reply
        0
        • K Kattia

          @Christian-Ehrlicher what rect the QPainter path rect? and how do you multiply a rect? only its width/height?

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #4

          @Kattia said in Problem with DPI and QIcon:

          what rect the QPainter path rect?

          The one you pass to QIcon::paint()

          and how do you multiply a rect? only its width/height?

          rect().size() * devicePixelRatio()

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved