Solved Draws Rect not from pos of the cursor (code provided)
-
This post is deleted! -
Hi
You might need to use
mapFromGlobal / maptoGLobal
translate from inside x,y to global rubberband x,yYou should check the docs if event->pos() is in global or local coordinates.
And then translate .
I guess its in local, so you want to translate to Global
as setGeometry is in global. -
@mrjj
The functions pos(), x(), and y() give the cursor position relative to the widget that receives the mouse event. - from docs
so I need to translate the event-pos() coordinates to global? -
@mandruk1331
Yes , since you use it with setGeometry then
you need to use mapToGlobal.
The x,y in setGeometry is global. -
@mrjj
like this:
origin = ui->label->mapFromGlobal(this->mapToGlobal(event->pos())); -
Hi im not sure it must be double mapped
More like
QPoint origin = this->mapToGlobal(event->pos()));
rubber->setGeometry(QRect(origin xxxx) -
@mrjj it's not drawing then. Is it better to use QRubberBand to draw an area of interest on the image, or QPaint?
-
@mandruk1331
Ok. Not sure why. Maybe Class is not what i think. ( the qlabel)Well with QRubberBand you can move it again as its not part of image.
So if it works well, nothing bad in using it. You could also draw it directly which is more
light but it becomes part of the image so its harder to undo/move/delete etc.
So depending on your needs, QRubberBand might be a good solution. -
@mrjj I DID IT!!! the problem was not in the code but in the UI. One more thing, in my UI i have a QLabel is there a way to draw the bounds of the label?
-
@mandruk1331
good work!
To draw borders on a QLabel, I think you need
to create new Widget (Qlabel as base class) and override PaintEvent.
Then first call base paint, and then draw the borders.Alternativ, you can use a frame, set FrameShape to "box"
insert layout to frame, then put label in layout.
set Layout margins for layout to zero (from 9)
It would then fit so tight that the label would have borders.
-
@mrjj Thanks. I overloaded the PaintEvent method and then I was able to draw a rect
-
@mandruk1331
Super. I think its the best solution. Also if you need other features, you now have your own QLabel derived class and can work from there.