
Originally Posted by
mvuori
The thing that gives you visibility to "overlay" in the event handlers is to make it a member variable of your MyWindow class. After that all functions of MyWindow will see it. As it is now, it is local of and only visible to the constructor of MyWindow.
That means that you also need to make updateRectangle() a member of MyWindow, that is, MyWindow::updateRectangle().
Wow, that is very good to know. Thank you. 
It now runs without erroring, however it doesn't work and I'm not sure why. The overlay appears on screen as expected, but when I click and drag, nothing at all happens. I drew a rectangle on screen earlier, but right here it doesn't work.
Here's my current code (it has a few changes):
start = event->globalPos();
end = event->globalPos();
updateRectangle();
}
end = event->globalPos();
updateRectangle();
}
end = event->globalPos();
updateRectangle();
}
void MyWindow::updateRectangle() {
p.setPen(Qt::blue);
p.drawRect(start.x(), start.y(), end.x() - start.x(), end.y() - start.y());
p.end();
//myLabel->setPixmap(overlay);
}
void MyWindow::mousePressEvent(QMouseEvent *event) {
start = event->globalPos();
end = event->globalPos();
updateRectangle();
}
void MyWindow::mouseMoveEvent(QMouseEvent *event) {
end = event->globalPos();
updateRectangle();
}
void MyWindow::mouseReleaseEvent(QMouseEvent *event) {
end = event->globalPos();
updateRectangle();
}
void MyWindow::updateRectangle() {
QPainter p(&overlay);
p.setPen(Qt::blue);
p.drawRect(start.x(), start.y(), end.x() - start.x(), end.y() - start.y());
p.end();
//myLabel->setPixmap(overlay);
}
To copy to clipboard, switch view to plain text mode
The overlay shows on screen but nothing happens when I click and drag. If I uncomment that line at the bottom, the overlay shows up but when I click the screens turns grey (as if the label is empty)
Bookmarks