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. Pixmap not scaling on hdpi
QtWS25 Last Chance

Pixmap not scaling on hdpi

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 2 Posters 1.1k Views
  • 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
    Kite R
    wrote on last edited by Kite R
    #1

    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
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #10

      @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
      1
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @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
        1
        • K Offline
          K Offline
          Kite R
          wrote on last edited by
          #3

          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
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @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
            0
            • Christian EhrlicherC Christian Ehrlicher

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

              getDevPixRatio()

              what does this function do?

              K Offline
              K Offline
              Kite R
              wrote on last edited by
              #5

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

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

                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
                0
                • Christian EhrlicherC Christian Ehrlicher

                  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.

                  K Offline
                  K Offline
                  Kite R
                  wrote on last edited by
                  #7

                  @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
                  0
                  • Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @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
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @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...

                      K Offline
                      K Offline
                      Kite R
                      wrote on last edited by
                      #9

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

                      QPixmap normalPixmap(input);
                      normalPixmap.setDevicePixelRatio(ratio);
                      
                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @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
                        1
                        • Christian EhrlicherC Christian Ehrlicher

                          @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.

                          K Offline
                          K Offline
                          Kite R
                          wrote on last edited by
                          #11

                          @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
                          0
                          • Christian EhrlicherC Online
                            Christian EhrlicherC Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            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
                            0

                            • Login

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