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:
MyWindow::MyWindow() {
//Up here i define the layout, label, etc. that all works correctly
QPixmap overlay;
//I define this correctly, but chose not to include the code because it is irrelevant myLabel->setPixmap(overlay);
}
start = event->globalPos();
updateRectangle();
}
end = event->globalPos();
updateRectangle();
}
end = event->globalPos();
updateRectangle();
}
void updateRectangle() { //This section does not work. I need to get the QPixmap "overlay" to here so I can update it.
p.setPen(Qt::blue);
p.drawRect(start.x(), start.y(), end.x() - start.x(), end.y() - start.y());
p.end();
}
QPoint start;
QPoint end;
MyWindow::MyWindow() {
//Up here i define the layout, label, etc. that all works correctly
QPixmap overlay; //I define this correctly, but chose not to include the code because it is irrelevant
myLabel->setPixmap(overlay);
}
void MyWindow::mousePressEvent(QMouseEvent *event) {
start = event->globalPos();
updateRectangle();
}
void MyWindow::mouseMoveEvent(QMouseEvent *event) {
end = event->globalPos();
updateRectangle();
}
void MyWindow::mouseReleaseEvent(QMouseEvent *event) {
end = event->globalPos();
updateRectangle();
}
void updateRectangle() { //This section does not work. I need to get the QPixmap "overlay" to here so I can update it.
QPainter p(&overlay);
p.setPen(Qt::blue);
p.drawRect(start.x(), start.y(), end.x() - start.x(), end.y() - start.y());
p.end();
}
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.
Bookmarks