PDA

View Full Version : Suggestions for drawing with mouse on GraphicsScene



GimpMaster
2nd April 2010, 03:52
I'm looking for suggestions on how to solve this problem. Basically what I have is a QGraphicsScene with a number of QGraphicPixmapItems. I want the user to be able to take the mouse and draw line trails (not straight lines), sort of like scribling over the image with a red Pen. I then need to be able to save off the trails they drew to a file so that it can be loaded again at a future point in time.

Would the best thing to do is create another GraphicsItem subclass that reads the mouse input and the paints the lines? Or should I subclass the Graphics Scene and add functionality in there?

Just curious what your thoughts are.

Thanks in advance.

wysota
2nd April 2010, 09:58
Your scribbling should be either a separate item or a series of line items. They should be created either by the scene or by the view as a result of handling mouse events in one of the two mentioned entities.

GimpMaster
2nd April 2010, 15:20
Do you think it would be sufficient to use the dragMoveEvent or a combination of (MousePress, MouseRelease, MouseMove) and then capture the mouse Delta and



NewLine = QGraphicsScene::addLine(...);
QList<QGraphicsLineItem*> LineList.push_back(NewLine);

// Later on write out the QList to a file


My only concern is that if they draw lots of curvy lines it will eat up tons of memory.

wysota
2nd April 2010, 16:07
Definitely not dragMoveEvent(). The other combination should be fine but I'd suggest to make all your scribble items children of some other item instead of making them directly children of the scene. But both approaches will work, it's just a matter of what you can (and want) do with the drawing.

GimpMaster
2nd April 2010, 16:09
Ok, I'm working on this now. I'll report back with my progress for future reference for others...

aamer4yu
2nd April 2010, 18:34
I guess you can also refer to the scribble example in Qt Demos :)

GimpMaster
2nd April 2010, 18:41
Yeah I found that one a while ago and I was looking for it yesterday. Thanks for the name hint.

It uses a QImage and performs line draws onto the QImage. I'm wondering if it would be better to use either:

a.) A list of QGraphicsLineItems that are drawn consecutively or,
b.) A QImage that is the size of the original image I'm drawing on and then do direct pixel modifications.

I suppose if there are few scribbles the memory requirement for the graphicslineitems would be much less, but if there are LOTs of scribles then QImage might be better?

wysota
2nd April 2010, 18:44
The fact that the demo works on an image doesn't mean you can't do the same on a widget (or whatever else QPainter can paint on).