title: Maintaining aspect ratio when painting large QImage to QWidget
body: <p>I'm working on an application that takes shows a live feed from an 18 Megapixel IDS uEye Camera.</p>
  
  <p>The three options that I have for displaying the image are 
  1) Using a Direct3D method from the API to draw to a QWidget using (HWND)QWidget->winId().
  2) Using a Bitmap Rendering method from the API to draw to a QWidget using (HWND)QWidget->winId().
  3) Creating a QImage from the raw sensor data (a char* buffer) and updating the GUI with the QImage.</p>
  
  <p>Because I would like to use the features of the QImage class for my application, I am currently pursuing that last option.</p>
  
  <p>I am able to successfully convert the image from the camera with the following code:</p>
  
  <pre><code>// Collect the image.
  int nRet = is_GetImageMem(m_hCam, &amp;m_pcLongImageMemory);
  if( nRet != IS_SUCCESS)
  {
      // handle error
  }
  
  //Put in unique_ptr&lt;&gt; to QImage
  img.reset((new QImage((uchar*)m_pcImageMemory, m_nSizeX, m_nSizeY, QImage::Format::Format_RGB888)));
  ui.widget-&gt;setImage(img.get());
  </code></pre>
  
  <p>This does fine in terms of generating the image that I will use. I then want to paint the image to a QWidget. For this, following advice from <a href="https://stackoverflow.com/a/22355028/4067133">this</a> thread, I have been able to successfully take these large, 18 MP images and update the GUI with them.</p>
  
  <p>So, my problem arises with the following method:</p>
  
  <pre><code>void ImageFrame::paintEvent(QPaintEvent*) {
    if (!m_image) { return; }
  
    QPainter painter(this);
    painter.drawImage(this-&gt;rect(), *m_image, m_image-&gt;rect());
  }   
  </code></pre>
  
  <p>My problem is with the adjustments made by the QPainter::paintImage() method being used. It seems that most of the overloads for ::paintImage() will automatically stretch the image to fit the bounding rectangle. In my case, I would prefer to maintain the 4:3 aspect ratio of my image at all times to avoid any distortion for the user. In this case, I have not found a QRect() configuration that will allow me to update the GUI with this image without stretching.</p>
  
  <p>It seems like my options are either to rescale the image to the size of the widget in the GUI (which seems very, very expensive with an image this size) or else to find a way to fix the size of this QWidget subclass so that it always scales into the maximum 4:3 ratio configuration it can within its surrounding layout...</p>
  
  <p>Any help or insight that someone might be able to provide is appreciated.</p>