Hi. I'm very new to C++ and Qt.

In my constructor, I have a QPixmap called "overlay" and I set a label's pixmap to overlay so that it displays on the screen.

I want to allow the user to draw a rectangle on the screen by clicking, dragging, and releasing. I did this exact thing in Java yesterday, so I understand how those mechanics work.

My problem is: I don't know how to pass the overlay QPixmap to my events so that they can update the rectangle. Here's is what is the relevant snippet of what currently have:

Qt Code:
  1. QPoint start;
  2. QPoint end;
  3.  
  4. MyWindow::MyWindow() {
  5. //Up here i define the layout, label, etc. that all works correctly
  6. QPixmap overlay; //I define this correctly, but chose not to include the code because it is irrelevant
  7. myLabel->setPixmap(overlay);
  8. }
  9.  
  10. void MyWindow::mousePressEvent(QMouseEvent *event) {
  11. start = event->globalPos();
  12. updateRectangle();
  13. }
  14.  
  15. void MyWindow::mouseMoveEvent(QMouseEvent *event) {
  16. end = event->globalPos();
  17. updateRectangle();
  18. }
  19.  
  20. void MyWindow::mouseReleaseEvent(QMouseEvent *event) {
  21. end = event->globalPos();
  22. updateRectangle();
  23. }
  24.  
  25. void updateRectangle() { //This section does not work. I need to get the QPixmap "overlay" to here so I can update it.
  26. QPainter p(&overlay);
  27. p.setPen(Qt::blue);
  28. p.drawRect(start.x(), start.y(), end.x() - start.x(), end.y() - start.y());
  29. p.end();
  30. }
To copy to clipboard, switch view to plain text mode 

As you can see, I really don't know what I'm doing. Right now I get "overlay: undeclared identifier" and if I put overlay into the global scope, I get an error saying I can't make a painter before an app.

Help would be greatly appreciated.