1
votes

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:

  1. Get the parameters of the rectangle user drawn. (height, width, center coordinate...)
  2. How to rotate an rectangle and get the rotation angle.
1
You're looking at your problem the wrong way around. How could your app draw the rectangle in the first place if it didn't have that info? Pick a way of drawing these rectangles, and a way for the user to manipulate them, and you'll have all you need. Look at the Qt examples & tutorials to get ideas.Mat
@Mat Hey Mat. Actually this is an app for manual annotation that the users should use the mouse to select area and the app can collect the information of this area, so this app will load an image first, then user select area. It is hard to implement that my app draw the rectangle and let user manipulate it, because finding "head" in one image is difficult, and this job should be done by user in this project.hakunami
Yes, precisely. Your app will handle the user's clicks and mouse moves over the image. It's your app that's going to draw the rectangles based on those clicks/moves/menu actions etc. You'll have the info you need.Mat
@Mat I get it. Thank you, I will try.hakunami
@Mat Hey, Mat. I collect the positions user clicked, and I try to use these information to create a rectangle, but I can't find a function to create a rectangle with four position. If I use these position information to calculate the width and height first, then choose a position to draw rectangle, I will abtain a rectangle but lose the rotate information (the rectangle will parallel with axises). So, how can I rotate a rectangle. I think a tricky way is drawing four lines with these positions, isn't it ?hakunami

1 Answers

1
votes

To get the rectangle user drawn, you need to get mouse events occuring in the drawing widget. You can reimplement mousePressEvent, mouseMoveEvent, mouseReleaseEvent of the widget or install event filter to this widget. When user presses left button, you should remember event's pos() as left-top corner of rectangle. When user moves mouse after that, you should set right-bottom corner of rectangle ro pos() 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.