PDA

View Full Version : Interesting QPainter Question... Not sure if its possible



Slewman
22nd February 2010, 19:45
Im sure your all familiar with the "Scribble" Qt demo included when you install the Qt SDK. My question is based off that demo.

Basically, drawing a pen over an image or QWidget is easy, set the pen to draw and track mouse events, not too hard. But what if i want to erase some of the drawings i made, but not all of them.

imagine i draw a giant line horizontally threw my image, and i want to erase small segments of the line, to produce a segmented line, but i dont want to do this simply by erasing rectangles (eraseRect), because the drawings i make on the image arent necessarily a straight line.

I had a few ideas how to do this:
1. as im drawing my "erasure" i call a get color on each pixel of the original image and just draw those colors over my new, "edited" image. The only issue with that is it can take up a lot of processor resources if you are checking the color at each pixel.

2. When i load the image i am trying to draw on, load it twice, and keep one image behind the other. When i draw, i am drawing over the front image, but when i want to erase, all i do is erase the forground image, which would then show the image behind it (unedited) thus making it look like i am erasing my custom pen marks.

basically, my question is how can i implement the second option, it seems to be the best way to do it, but im not sure how to erase anything with QPainter except for the eraseRect call.

Any suggestions are greatly appreciated! thanks for the help :)

EDIT: Alright, it seems that eraseRect() would work for me, except that it also erases the background image i am drawing... is there anyway i can set the background image (QImage) to be un-editable for a short period of time?

boudie
22nd February 2010, 21:30
You could store every item (line, rectangle etc.) as a QPainterPath allowing you to redraw the image completely, partially or modified. Simply adjust some properties of an object to modify the picture.

Slewman
22nd February 2010, 21:39
yea i tried that, it didnt exactly give me the behavior i was looking for.

i really think the best way to do it would be to draw 2 images on top of each other. the one in the forground would be the one i could draw onto, and the one in the background would just be there to fill in the spots that i erase from the forground picture. does this make sense? how can i go about drawing 2 images like this?