Results 1 to 6 of 6

Thread: drawLine with pen in background color ("erase functionality")

  1. #1
    Join Date
    Nov 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default drawLine with pen in background color ("erase functionality")

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: drawLine with pen in background color ("erase functionality")

    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,
    _

  3. #3
    Join Date
    Nov 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: drawLine with pen in background color ("erase functionality")

    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).

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: drawLine with pen in background color ("erase functionality")

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Nov 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: drawLine with pen in background color ("erase functionality")

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: drawLine with pen in background color ("erase functionality")

    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,
    _

Similar Threads

  1. Replies: 1
    Last Post: 20th November 2015, 10:02
  2. QSqlError("", "Parameter count mismatch", "")
    By Alberto7 in forum Newbie
    Replies: 2
    Last Post: 9th October 2015, 22:09
  3. Replies: 3
    Last Post: 16th March 2015, 07:31
  4. Replies: 7
    Last Post: 3rd May 2013, 17:03
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.