title: QGraphicsView scrolling and image scaling/cropping
body: <p>I would like to have a background image in my QGraphicsView that is always scaled (and cropped if necessary) to the size of the viewport, without scrollbars and without scrolling with the keyboard and mouse. The example below is what I am doing to scale and crop an image in the viewport, but I am using random values for the cropping that are pulled out of the aether. I would like a logical solution?</p>
  
  <pre><code>MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
  
      ui-&gt;setupUi(this);
      scene = new QGraphicsScene(this);
  
      ui-&gt;graphicsView-&gt;resize(800, 427); 
      // MainWindow is 800x480, GraphicsView is 800x427. I want an image that
      // is the size of the graphicsView.
  
      ui-&gt;graphicsView-&gt;setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      ui-&gt;graphicsView-&gt;setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
      // the graphicsView still scrolls if the image is too large, but 
      // displays no scrollbars. I would like it not to scroll (I want to 
      // add a scrolling widget into the QGraphicsScene later, on top of
      // the background image.)
  
  
      QPixmap *backgroundPixmap = new QPixmap(":/Valentino_Bar_Prague.jpg");
      QPixmap sized = backgroundPixmap-&gt;scaled(
              QSize(ui-&gt;graphicsView-&gt;width(), 
                    ui-&gt;graphicsView-&gt;height()),
              Qt::KeepAspectRatioByExpanding); // This scales the image too tall
  
      QImage sizedImage = QImage(sized.toImage());
      QImage sizedCroppedImage = QImage(sizedImage.copy(0,0,
         (ui-&gt;graphicsView-&gt;width() - 1.5),
         (ui-&gt;graphicsView-&gt;height() + 19))); 
      // so I try to crop using copy(), and I have to use these values
      // and I am unsure why.
  
      QGraphicsPixmapItem *sizedBackground = scene-&gt;addPixmap(
          QPixmap::fromImage(sizedCroppedImage));
      sizedBackground-&gt;setZValue(1);
      ui-&gt;graphicsView-&gt;setScene(this-&gt;scene);
  }
  </code></pre>
  
  <p>I would like to know a way to scale and crop an image to the size of the QGraphicsView that will work even when I resize the QGraphicsView. Where are the 1.5 and 19 coming from?</p>
  
  <p>EDIT; I have also attempted to use setBackgroundBrush, but I get a tiled background, even when using the scaled/cropped QImage/QPixmap.</p>
  
  <p>EDIT; My solution thus far has been to override drawBackground() to get the result I wanted, but this still doesn't help me learn how to size an image to the viewport size of a qgraphicsview. Any further answers would be greatly appreciated.</p>
  
  <pre><code>void CustomGraphicsView::drawBackground( QPainter * painter, const QRectF &amp; rect )
  {
  
      qDebug() &lt;&lt; "background rect: " &lt;&lt; rect &lt;&lt; endl;
  
      QPixmap *backgroundPixmap = new QPixmap(":/Valentino_Bar_Prague.jpg");
      QPixmap sized = backgroundPixmap-&gt;scaled(QSize(rect.width(), rect.height()), Qt::KeepAspectRatioByExpanding);
  
      painter-&gt;drawPixmap(rect, sized, QRect(0.0, 0.0, sized.width(), sized.height()));
  
  }
  </code></pre>