title: How can I draw a selection rectangle on QScrollArea?
body: <p>I have created an Image Viewer application that opens and saves an image and load the image on QLabel,then I created a ScrollArea to scroll for large images, in my second step I am trying to draw a selection rectangle to select a specific sub area , the steps I have taken to paint the selection rectangle as follows:</p>
  
  <p>1- I used an PaintEvent to paint the rectangle.
  2- I used MouseEvent to select the sub area.</p>
  
  <p>The <strong>problem</strong> is, when I run my code, I can draw the rectangle on the QWidget but not on the ScrollArea.</p>
  
  <p>Here is my code:</p>
  
  <p>in imageviewer.h</p>
  
  <pre><code>class ImageViewer : public QWidget{
  Q_OBJECT
  public:
     explicit ImageViewer(QWidget *parent = 0);
     ~ImageViewer();
  private:
     Ui::ImageViewer *ui;
  
  private slots:
    void on_openButton_pressed();
  
    void on_saveButton_pressed();
  
  private:
    QPixmap image;
    QImage *imageObject;
    bool selectionStarted;
    QRect selectionRect;
    QMenu contextMenu;
  
  protected:
    void paintEvent(QPaintEvent *e);
    void mousePressEvent(QMouseEvent *e);
    void mouseMoveEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent *e);
  };
  </code></pre>
  
  <p>this is imageviewer.cpp</p>
  
  <pre><code>ImageViewer::ImageViewer(QWidget *parent) :
      QWidget(parent),
      ui(new Ui::ImageViewer)
  {
      ui-&gt;setupUi(this);
      ui-&gt;scrollArea-&gt;setWidget(ui-&gt;imageLabel);
  }
  </code></pre>
  
  <p>open &amp; save functions:</p>
  
  <pre><code>void ImageViewer::on_openButton_pressed()
  {
      QString imagePath = QFileDialog::getOpenFileName(this, tr("Open File") , "" ,
                                                   tr("JPEG (*.jpg *.jpeg);;PNG (*.png);;BMP (*.bmp)"));
  
      imageObject = new QImage();
      imageObject-&gt;load(imagePath);
  
      image = QPixmap::fromImage(*imageObject);
  
      ui-&gt;imageLabel-&gt;setPixmap(image);
      ui-&gt;imageLabel-&gt;adjustSize();
  
  }
  
  void ImageViewer::on_saveButton_pressed()
  {
      QString imagePath = QFileDialog::getSaveFileName(this, tr("Save File") , "" ,
                                                   tr("JPEG (*.jpg *.jpeg);;PNG (*.png);;BMP (*.bmp)"));
  
      *imageObject = image.toImage();
      imageObject-&gt;save(imagePath);
  }
  </code></pre>
  
  <p>paint &amp; mouse event functions:</p>
  
  <pre><code>void ImageViewer::paintEvent(QPaintEvent *e){
      QWidget::paintEvent(e);
      QPainter painter(this);
      painter.setPen(QPen(QBrush(QColor(0,0,0,180)),1,Qt::DashLine));
      painter.setBrush(QBrush(QColor(255,255,255,120)));
      painter.drawRect(selectionRect);
  }
  
  void ImageViewer::mousePressEvent(QMouseEvent *e){
      if(e-&gt;button() == Qt::RightButton){
          if(selectionRect.contains(e-&gt;pos()))
              contextMenu.exec(this-&gt;mapToGlobal(e-&gt;pos()));
      }
      else{
          selectionStarted = true;
          selectionRect.setTopLeft(e-&gt;pos());
          selectionRect.setBottomRight(e-&gt;pos());
      }
  }
  
  void ImageViewer::mouseMoveEvent(QMouseEvent *e){
      if(selectionStarted){
          selectionRect.setBottomRight(e-&gt;pos());
          repaint();
      }
  }
  
  void ImageViewer::mouseReleaseEvent(QMouseEvent *e){
      selectionStarted = false;
  }
  </code></pre>
  
  <p>and this is a screenshot for my application</p>
  
  <p><a href="https://i.stack.imgur.com/S5j4J.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/S5j4J.png" alt="enter image description here"></a></p>