PDA

View Full Version : Scribble to GraphicsView



rdelgado
10th November 2009, 20:06
Hello,

I was wondering if it is possible, in Qt, to convert a scribbled paint into a QGraphicsItem.

What I am trying to do is a scribble area where, for example, I draw a circle with the mouse, and then after the circle is finish and I release the mouse button, then that figure is
somehow converted into an item I can drag, rotate, and treat as an individual item. The same way, I want to be able to draw a square, and then use that square as a GraphicsView item, get the bounding rect, move it, use collision detection, etc.

Do you think it is possible to do that in Qt?
Do you have any suggestions?

Thank you very much!

scascio
11th November 2009, 00:27
Since you can override any mouse event of your QGraphicsView, and if your mind is clear, of course you can do what you want.
May be it is not so easy and you might need some well defined design.

For instance, you can design a drawing tool object, one instance for the circle scribble and another for rect one. A tool object, when activated in an exlusive way, will receive mouse events.

So you can manage a temporary item with the activated tool that will set a QGraphicsItem according to mouse position and click. So, on right click for example, get the temporary geometry.
Then you can create your items and set their flags for your needs.

JohannesMunk
11th November 2009, 14:35
Hi!

To get started, you could look into the Qt-boxes demo (http://doc.trolltech.com/4.5/demos-boxes.html). The relevant classes are implemented in qtbox.h/cpp.

There you can see rectangles and circles been created, moved around and resized with the mouse.

You will probably want to create your items in QGraphicsScene::mousePressEvent (http://doc.trolltech.com/latest/qgraphicsscene.html#mousePressEvent), and not when you select the tool.

Or did I miss the point and you want to really scrible with always the same tool/pen, and you want your application to detect afterwards if its a rectangle or a circle and convert it into the appropriate item?

For that you will need to keep track of the pens path. Find its geometric center, width and height. Implement an errorfunction, that will give you a score of how good the path matches to a rectangular or an ellipse/circle with those parameters. Maybe go through all the points of your path and add up the absolute distance to the nearest point of the testshape. The one with the lowest score wins then.. I don't think there is a prebuild funtion if thats what you were asking.

Let me know if you need further help.

HIH

Johannes

rdelgado
11th November 2009, 15:52
Hi,

Thanks for your help!


Or did I miss the point and you want to really scrible with always the same tool/pen, and you want your application to detect afterwards if its a rectangle or a circle and convert it into the appropriate item?

Yes, I want to scribble with the same tool and my application to detect it. I could make my application detect the object and convert it to a shape-item, but what I really want to do is to keep using the scribbled figure as an item.

I want to make an application similar to Crayon Physics or Numpty Physics and other physics based games, and that is why I wish to know if such functionality is possible on Qt.

I'll keep studying Qt and working around to see if I can make it.

Thank you very much!