drawing an rectangle above a picture in a QLabel
hi,
i have a QLabel and loaded a picture in it. Now i'd like to realize that a user can select a part of the picture. Therefore i captured the mouseevents over the label and could save the selection in another QLabel (like clipping).
So far, everything works fine, but now i want to visualize the selection the user is doing while he has pressed the mouse over the picture. I tried using a QPainter like this:
QRectF rectangle(leftUpperX, leftUpperY, rightLowerX, rightLowerY);
QPainter painter(this);
painter.drawRect(rectangle);
In the mousemove-event i got no functionality and this error:
QPainter::begin: Widget painting can only begin as a result of a paintEvent
So i tried to put in into an paint-Event:
void InitialLabel::paintEvent(QPaintEvent* event) {
QRectF rectangle(10.0, 20.0, 200.0, 200.0);
QPainter painter(this);
painter.drawRect(rectangle); // drawing code
}
But now the picture is no longer shown, only the rectangle.:o
What i would like is a variable rectangle while the mouse button is held that fixes it's position when the mouse button is released.
This should be on top of the picture, so that a user can see the picture in the background while he makes his selection.
Anyone who can help me with this?
Thanks in advance.
Re: drawing an rectangle above a picture in a QLabel
Yes.
You should load the pixmap you want to draw, and in paintEvent first draw the pixmap then the rectangle over it, just as you did.
The reason the pixmap disappeared is because you've overriden the paintEvent.
Another solution is to leave your code just asi it is now, but before you draw anything in paint event, add the line QLabel:: paintEvent( event ). This way the base class will draw it's contents, and you can perform any custom drawing that you please.
Regards
Re: drawing an rectangle above a picture in a QLabel
First of all QLabel inherits QFrame, so you can just adjust the frameShape property. But to answer your question, you have to call the base class implementation to see both your drawing and the original one.
Re: drawing an rectangle above a picture in a QLabel
I would suggest installing a filter on mousemovements and draw the rectangle using a rubberband. This will help u draw the rectangle independant of the picture being drawn.
See QRubberBand for this... it will help u select the area in picture with the area being semi transparent showing the picture beneath it.
Hope this helps :)