Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPainter / QPixmap : problem erasing antialiased lines
Qt 6.11 is out! See what's new in the release blog

QPainter / QPixmap : problem erasing antialiased lines

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 3.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • marievinM Offline
    marievinM Offline
    marievin
    wrote on last edited by marievin
    #1

    Hello,

    I would like to erase a previously drawn line in a QPixmap. Here is little sample code to demonstrate the problem:

    void Widget::paintEvent(QPaintEvent *)
    {
        QPainter p(this);
        QPixmap px(size());
    
        px.fill(Qt::transparent);
    
        QPainter p2(&px);
        p2.setRenderHint(QPainter::Antialiasing, true);
        p2.setPen(Qt::red);
        p2.drawLine(100, 100, 200, 200);
        p2.setCompositionMode(QPainter::CompositionMode_Clear);
        p2.drawLine(100, 100, 200, 200);
    
        p2.end();
    
        p.drawPixmap(0, 0, px);
        // no line should be drawn in the widget
    }
    

    When I run this code, the widget contains a semi transparent red line.
    Here is the screenshot

    If I deactivate the antialiasing, everything works fine : the line is properly erased from the pixmap.

     p2.setRenderHint(QPainter::Antialiasing, false);
    

    Anybody can tell me what I'm doing wrong ?

    Thank you in advance,
    Best regards
    Vincent

    1 Reply Last reply
    0
    • m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by
      #2

      Hi,
      probably it should work as you expects. But as it does not I would resort to the usual way to do this:
      if you want the transparent line removed from the widget simply draw the whole widget anew without the line.
      -Michael.

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        Not tested myself but did you try with
        setCompositionMode(QPainter::RasterOp_SourceXorDestination);

        1 Reply Last reply
        2
        • m.sueM Offline
          m.sueM Offline
          m.sue
          wrote on last edited by
          #4

          Yes, Xor-mode is the other way (a fast way) to do this. Then you would already draw the original line in Xor-mode and redraw it in Xor-mode in exactly the same way as before to get rid of it again.
          But beware: lines drawn in Xor-mode look in a way your users have to get accustomed to :-)

          1 Reply Last reply
          1
          • marievinM Offline
            marievinM Offline
            marievin
            wrote on last edited by marievin
            #5

            Thank you for your help,

            @m-sue
            For performance reasons, I cannot redraw the entire widget. I draw a lot of line in a pixmap (representing the curve of an analogic signal over time : it can be one hour of data). I optimise my code to only draw needed lines but sometimes I have to redraw a line to reflect signal changes.

            @mrjj
            As Qt doc says turning on the QPainter::Antialiasing render hint will effectively disable the RasterOp modes. So I cannot use this flag to erase the line

            Regards
            Vincent

            1 Reply Last reply
            1
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Oh, Its simply not supported to
              mix Antialiasing and xor style.
              Ok no use then. Was just a thought.
              Good you do in fact read the docs :)

              If full repaint is too expensive, what about dirty rect ? If you need to redraw a line, delete bounding box
              then you redraw all lines affected. (also if partial in .)

              1 Reply Last reply
              3

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved