PDA

View Full Version : drawLine with pen in background color ("erase functionality")



TriGemini
26th November 2016, 09:13
I want to accomplish the following:

1. I have a CustomView class that inherits the GraphicsView with background style set to being transparent - setStyleSheet("background: transparent")
2. To the CustomView I add a QGraphicsScene and draw a thick Line via scene->addLine
3. Now I want to erase part of the red line, by simply drawing a new line in the scene with a transparent color (to mimick erase of parts of the red line).

Is this possble and how?

I've tried to implement a CustomLine inheriting from a QGraphicsLineItem and implementing the paintevent to play around with compositionmode, however with no usefull result.

anda_skoa
26th November 2016, 09:41
If your line has a "hole" then it is not a line anymore.

You could try a painter path item, QPainterPath can draw shapes with holes.

Cheers,
_

TriGemini
26th November 2016, 14:36
Hi Anda,

You're right - but I'm going to use it in a paint-like program where you paint a line (vector) and must be able to delete parts of it using another line (erase).

d_stranz
26th November 2016, 18:48
must be able to delete parts of it using another line (erase)

If you continue to think that your problem is a nail, then you will only come up with solutions that require hammers.

Anda_skoa has pointed out one possible solution: Instead of locking yourself into thinking that you must draw a new line to erase part of your old line, you break your existing line into two parts and compose a QPainterPath that consists of two lines with a gap in between them.

TriGemini
27th November 2016, 09:23
I agree, however this method will not solve my problem. Imagine that you draw a line with a thickness of 30 and you want to erase with a line of thickness 10 in the middle of the line, that your method wouldn't solve. Further more it would open for even more complicated algorithms, such as multiple crossing lines would require all to be split etc.

I've been through numerous of different ways to solve it, but if I just could paint in a transparent color - or an alternative to eraseRect (I guess) in a line-form, this would be a solution.

anda_skoa
27th November 2016, 12:57
If you have items that are more complex than what a QPainterPath can handle (your example would still work I think), then you need to use a pixmap item and compose the required result.

Cheers,
_