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. Has devicePixelRatio behavior changed for 5.8?

Has devicePixelRatio behavior changed for 5.8?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.2k Views 3 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
    krekeltronics
    wrote on last edited by krekeltronics
    #1

    I pull images into a QPixmap like so:

    const qreal pixelRatio = ((QGuiApplication*)QCoreApplication::instance())->devicePixelRatio();
        p = p.scaled(w*pixelRatio, h*pixelRatio, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        p.setDevicePixelRatio(pixelRatio);
        QIcon pixmapIcon(p);
    

    I'm not sure if this is the perfect method, but up to Qt 5.7 it resulted in a HiDPI pixmap that I was able to use at width/pixelRatio, height/pixelRatio in the UI. Now, I end up with a fuzzy bitmap no matter what which if I change nothing is twice the size as the containing widget (and clearly not HiDPI). Has the HiDPI behavior changed fundamentally for 5.8? I didn't see any difference in the documentation.

    Cheers!

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      For what it's worth, I noticed the "fuzzyness" in 5.8 even without devicePixelRatio when I create a QIcon from a QPixmap so I think the problem is QIcon pixmapIcon(p);

      P.S.
      Maybe this is related: https://bugreports.qt.io/browse/QTBUG-44486

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      K 2 Replies Last reply
      0
      • VRoninV VRonin

        For what it's worth, I noticed the "fuzzyness" in 5.8 even without devicePixelRatio when I create a QIcon from a QPixmap so I think the problem is QIcon pixmapIcon(p);

        P.S.
        Maybe this is related: https://bugreports.qt.io/browse/QTBUG-44486

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

        @VRonin that's really interesting. Oddly enough, it looks really nice and sharp on 5.5-5.7 with our method, sized properly and everything. We just scale it to the pixel ratio (in this case 2X for a HiDPI Mac), then size the container to the "points" width and height.

        I do think there are some wonky things in QPixmap and QIcon so maybe our implementation worked by accident before and now needs to be done a different way. Does anyone have this consistently working?

        1 Reply Last reply
        0
        • VRoninV VRonin

          For what it's worth, I noticed the "fuzzyness" in 5.8 even without devicePixelRatio when I create a QIcon from a QPixmap so I think the problem is QIcon pixmapIcon(p);

          P.S.
          Maybe this is related: https://bugreports.qt.io/browse/QTBUG-44486

          K Offline
          K Offline
          krekeltronics
          wrote on last edited by
          #4

          @VRonin The icon size is now correct, and sharp, using the above icon+pixmap initialization along with this with the resulting icon. I call setIconToSize with the pixmap and the display w/h of the container it's going into. I don't use the @2x filename trick since this is generating the pixmap programmatically using an image the user selects, which ends up on a back end:

          void SSImageButton::setIconToSize(QPixmap p, int w, int h)
          {
              QIcon icon = Utilities::getIconForPixmap(p, w, h);
              QSize iconSize = QSize(w, h);
              this->setIconSize(iconSize);
              this->setIcon(icon);
          }
          

          where getIconForPixmap is as follows:

          QIcon Utilities::getIconForPixmap(QPixmap p, int w, int h)
          {
              QRect cropRect = QRect(0, 0, p.width(), p.height());
              if (p.height() > p.width()) {
                  cropRect.setHeight(cropRect.width() * h / w);
                  cropRect.moveTop((p.height() - cropRect.height())/2);
                  p = p.copy(cropRect);
              } else if (p.height() < p.width()) {
                  cropRect.setWidth(cropRect.height() * w / h);
                  cropRect.moveLeft((p.width() - cropRect.width())/2);
                  p = p.copy(cropRect);
              } else {
                  // it is square, we are all set
              }
          
              const qreal pixelRatio = ((QGuiApplication*)QCoreApplication::instance())->devicePixelRatio();
              p = p.scaled(w*pixelRatio, h*pixelRatio, Qt::KeepAspectRatio, Qt::SmoothTransformation);
              p.setDevicePixelRatio(pixelRatio);
              QIcon pixmapIcon(p);
              return pixmapIcon;
          }
          
          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