I want to develop a small application that allows users using rectangles to annotate different parts of an image. For example, user can draw a rectangle over an image labeled “head” and this app can get the parameters of this rectangle (height, width, center coordinate…).
Sometimes I also need to rotate this rectangle to choose a particular area.
I want to use Qt to implement my idea, but I don’t know how to:
- Get the parameters of the rectangle user drawn. (height, width, center coordinate…)
- How to rotate an rectangle and get the rotation angle.
To get the rectangle user drawn, you need to get mouse events occuring in the drawing widget. You can reimplement mousePressEvent,
mouseMoveEvent,mouseReleaseEventof the widget or install event filter to this widget. When user presses left button, you should remember event’spos()as left-top corner of rectangle. When user moves mouse after that, you should set right-bottom corner of rectangle ropos()of move event. Releasing the button must commit creating a rectangle.There are several ways to draw rectangles. You can implement paintEvent and use QPainter inside of it. But I think the best way is to use QGraphicsScene. You can create visible rectangles, move and rotate them.