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

QGraphicsPixmapItem bad bounding box when pixamap with device pixel ratio



    • Commits:
      795af729d38343e16b0902323609cd1e959a5973 (qt/qtbase/5.12)


      When setDevicePixelRatio is set to a QPixmap, an then this QPixmap is used in a QGraphicsPixmap item, the bounding box is not computed properly. The current implementation ends up dividing a QSize by a qreal returning a QSize instead of a QSizeF. This produces truncation problems making the bounding box not contain exactly the scaled QPixmap:

      QRectF QGraphicsPixmapItem::boundingRect() const
        Q_D(const QGraphicsPixmapItem);
        if (d->pixmap.isNull())
          return QRectF();
        if (d->flags & ItemIsSelectable) {
          qreal pw = 1.0;
          return QRectF(d->offset, d->pixmap.size() / d->pixmap.devicePixelRatio()).adjusted(-pw/2, -pw/2, pw/2, pw/2);
         } else {
           return QRectF(d->offset, d->pixmap.size() / d->pixmap.devicePixelRatio());

      In this repository(https://github.com/apalomer/QGraphicsView_bounding_box_bug_solution) there is a CustomGraphicsPixmapItem that inherits from QGraphicsPixmapItem and has the default way of computing the bounding box and the one that solves the previously described issue.  If the code is compiled and run, the control on the right allows to move the image in the graphics view. The combo box sets the bounding box computation method. If set to "Default Qt" and the image is moved with the X and Y spin boxes, we can see that some parts of the image are left in the view. If the combo box is set to "Custom impl" this is not happening anymore (note that the left overs from previous movement will not be removed unless View->Fit is clicked because it produces a repaint of the current display rectangle). See the attached video for example.

      The solution is to implement the bounding rectangle function the following way :

      QRectF CustomGraphicsPixmapItem::boundingRect() const
        if (QGraphicsPixmapItem::pixmap().isNull())
          return QRectF(); 
        QSizeF szf(QGraphicsPixmapItem::pixmap().size().width(), QGraphicsPixmapItem::pixmap().size().height());
        if (QGraphicsPixmapItem::flags() & ItemIsSelectable) { 
          qreal pw = 1.0;
          return QRectF(QGraphicsPixmapItem::offset(), szf / QGraphicsPixmapItem::pixmap().devicePixelRatio()) .adjusted(-pw / 2, -pw / 2, pw / 2, pw / 2);
        } else {
          return QRectF(QGraphicsPixmapItem::offset(), szf / QGraphicsPixmapItem::pixmap().devicePixelRatio());


        No reviews matched the request. Check your Options in the drop-down menu of this sections header.



            • Assignee:
              bibr Andreas Aardal Hanssen
              apalomer Albert Palomer
            • Votes:
              0 Vote for this issue
              0 Start watching this issue


              • Created:

                Gerrit Reviews

                There are no open Gerrit changes