Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Pixmap not scaling on hdpi

    General and Desktop
    2
    12
    486
    Loading More Posts
    • 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
      Kite R last edited by Kite R

      Hi, I have a function to lower opacity of pixmaps to use as icon disabled states but the returned pixmaps display very tiny on a hdpi monitor, all other pixmaps display fine.

      QPixmap generateOpacity(QPixmap &input) {
        QPixmap normalPixmap(input);
        QPixmap disabledPixmap(normalPixmap.size());
      
        disabledPixmap.fill(Qt::transparent);
      
        QPainter p(&disabledPixmap);
        p.setBackgroundMode(Qt::TransparentMode);
        p.setBackground(QBrush(Qt::transparent));
        p.eraseRect(normalPixmap.rect());
        p.setOpacity(0.2);
        p.drawPixmap(0, 0, normalPixmap);
        p.end();
      
        return disabledPixmap;
      }
      

      And I use it as so...

      QIcon normal;
      normal = QIcon::fromTheme("icon");
      QPixmap normalPixmap = normal.pixmap(22, 22);
      QPixmap disabledPixmap = generateOpacity(normalPixmap);
      
      icon.addPixmap(normalPixmap, QIcon::Normal, QIcon::Off); // displays correctly
      icon.addPixmap(disabledPixmap, QIcon::Disabled);         // displays tiny
      

      How come pixmaps that run through this function don't display correctly?

      1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        @Kite-R said in Pixmap not scaling on hdpi:

        Setting this does the same as before, tiny pixmaps.

        You have to scale it up - otherwise you only create a big pixmap with a small icon inside.

        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 Reply Quote 1
        • Christian Ehrlicher
          Christian Ehrlicher Lifetime Qt Champion last edited by

          @Kite-R said in Pixmap not scaling on hdpi:

          How come pixmaps that run through this function don't display correctly?

          QPixmap::setDevicePixelRatio()

          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 Reply Quote 1
          • K
            Kite R last edited by

            That works on pixmaps generated from QIcon (mostly, there are still some with issues), but it only very slightly increases the size from pixmaps generated from Qicon::fromTheme.

            QPixmap generateOpacity(QPixmap &input) {
              QPixmap normalPixmap(input);
              normalPixmap.setDevicePixelRatio(getDevPixRatio());
            
              QPixmap disabledPixmap(normalPixmap.size());
              disabledPixmap.setDevicePixelRatio(getDevPixRatio());
            
              disabledPixmap.fill(Qt::transparent);
            
              QPainter p(&disabledPixmap);
            
              p.setBackgroundMode(Qt::TransparentMode);
              p.setBackground(QBrush(Qt::transparent));
              p.eraseRect(normalPixmap.rect());
              p.setOpacity(0.2);
              p.drawPixmap(0, 0, normalPixmap);
              p.end();
            
              return disabledPixmap;
            }
            
            1 Reply Last reply Reply Quote 0
            • Christian Ehrlicher
              Christian Ehrlicher Lifetime Qt Champion last edited by

              @Kite-R said in Pixmap not scaling on hdpi:

              getDevPixRatio()

              what does this function do?

              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 Reply Quote 0
              • K
                Kite R @Christian Ehrlicher last edited by

                @Christian-Ehrlicher Oh, forgive me for missing that out.

                int getDevPixRatio() {
                  static int devPixRatio = QApplication::desktop()->devicePixelRatio();
                  return devPixRatio;
                }
                
                1 Reply Last reply Reply Quote 0
                • Christian Ehrlicher
                  Christian Ehrlicher Lifetime Qt Champion last edited by

                  You forgot to multiply the pixel size of the pixmap with the device pixel ratio:

                  For example, painting on a 200x200 image if with a ratio of 2.0 will result in effective (device-independent) painting bounds of 100x100.

                  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 Reply Quote 0
                  • K
                    Kite R @Christian Ehrlicher last edited by

                    @Christian-Ehrlicher Hmm, I tried this but it produces corrupted looking pixmaps, most of them display nothing. Is this right?

                    QPixmap generateOpacity(QPixmap &input) {
                      qreal ratio = getDevPixRatio();
                      QPixmap disabledPixmap(input.width() * ratio, input.height() * ratio);
                      disabledPixmap.setDevicePixelRatio(ratio);
                      disabledPixmap.fill(Qt::transparent);
                    
                      QPixmap normalPixmap(input.width() * ratio, input.height() * ratio);
                      normalPixmap.setDevicePixelRatio(ratio);
                    
                      QPainter p(&disabledPixmap);
                      p.setBackgroundMode(Qt::TransparentMode);
                      p.setBackground(QBrush(Qt::transparent));
                      p.eraseRect(normalPixmap.rect());
                      p.setOpacity(0.2);
                      p.drawPixmap(0, 0, normalPixmap);
                      p.end();
                    
                      return disabledPixmap;
                    }
                    
                    1 Reply Last reply Reply Quote 0
                    • Christian Ehrlicher
                      Christian Ehrlicher Lifetime Qt Champion last edited by

                      @Kite-R said in Pixmap not scaling on hdpi:

                      most of them display nothing. Is this right?

                      QPixmap normalPixmap(input.width() * ratio, input.height() * ratio);

                      You create an empty pixmap...

                      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 Reply Quote 0
                      • K
                        Kite R @Christian Ehrlicher last edited by

                        @Christian-Ehrlicher Setting this does the same as before, tiny pixmaps.

                        QPixmap normalPixmap(input);
                        normalPixmap.setDevicePixelRatio(ratio);
                        
                        1 Reply Last reply Reply Quote 0
                        • Christian Ehrlicher
                          Christian Ehrlicher Lifetime Qt Champion last edited by

                          @Kite-R said in Pixmap not scaling on hdpi:

                          Setting this does the same as before, tiny pixmaps.

                          You have to scale it up - otherwise you only create a big pixmap with a small icon inside.

                          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 Reply Quote 1
                          • K
                            Kite R @Christian Ehrlicher last edited by

                            @Christian-Ehrlicher Aaah... thanks so much man, it's been a long day, this worked:

                            QPixmap normalPixmap = input.scaled(input.width() * ratio, input.height() * ratio, Qt::KeepAspectRatio);
                            
                            1 Reply Last reply Reply Quote 0
                            • Christian Ehrlicher
                              Christian Ehrlicher Lifetime Qt Champion last edited by

                              Yeah, the high-dpi stuff is not easy... even in the qt styles there are still a lot of pitfalls, esp. due to rounding errors.

                              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 Reply Quote 0
                              • First post
                                Last post