Results 1 to 5 of 5

Thread: QPainter::setCompositingMode without effect

  1. #1
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QPainter::setCompositingMode without effect

    Hi!

    I am developping my own QGraphicsItem that should paint a partly transparent QImage. This is easy, it can just be realised via

    Qt Code:
    1. void GraphicsHairstyleItem::paint(QPainter *painter,
    2. const QStyleOptionGraphicsItem *option, QWidget *widget)
    3. {
    4. Q_UNUSED(option);
    5. Q_UNUSED(widget);
    6.  
    7. painter->drawImage(m_imgrect, m_img_warped);
    8. }
    To copy to clipboard, switch view to plain text mode 

    OK. But now I qould like to be able to realise something like "erasing", or like the rubber-tool in Photoshop. Therefor I inted to have a second QImage called the "cutmask" that is transparent everywhere except for parts of the image that should be erased.

    Following the documentation on Compositing modes, if the image to be shown is the destination and the cutmask acts as the source (but I'm not a 100% sure about this), then QPainter::CompositionMode_DestinationOut should do the right thing.

    This means that only portions of the image where the cutmask is transparent are shown.

    So my code looks like

    Qt Code:
    1. void GraphicsHairstyleItem::paint(QPainter *painter,
    2. const QStyleOptionGraphicsItem *option, QWidget *widget)
    3. {
    4. Q_UNUSED(option);
    5. Q_UNUSED(widget);
    6.  
    7. painter->drawImage(m_imgrect, m_img_warped);
    8. painter->setCompositionMode(QPainter::CompositionMode_DestinationOut);
    9. painter->drawImage(m_imgrect, m_img_cutmask);
    10. }
    To copy to clipboard, switch view to plain text mode 

    see the attachments for my cutmask image "cut.png".

    But in this realisation (and also in many others), the white spot is just drawn on top of the image, instead of erasing it out there.

    Thank you very much for your help in advance,
    Olli
    Attached Images Attached Images
    Last edited by olidem; 4th March 2009 at 20:10. Reason: reformatted to look better

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QPainter::setCompositingMode without effect

    Quick guess: is anti aliasing turned off? And what happens if you code your paint function like this of the image composition example?

    (And have you notified that "The RasterOp denoted blend modes are not supported for pens and brushes with alpha components."?)

  3. #3
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPainter::setCompositingMode without effect

    Hmm, ok, I do not think that there is a connection to antialiasing.

    The only idea I have is about the the format of the QImages. In the referred example, all images are kept in a premultiplied format (see the loadImage function), but I think this is mainly for rescaling reasons.

    The problem I have is that the first step of there rendering process always at first fills the whole image with transparent color
    Qt Code:
    1. painter.setCompositionMode(QPainter::CompositionMode_Source);
    2. painter.fillRect(resultImage.rect(), Qt::transparent);
    To copy to clipboard, switch view to plain text mode 

    which I can't do because I would delete other things.

    What about the attached file? Do you see errors there?

  4. #4
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPainter::setCompositingMode without effect

    News:

    I have now converted everything into the premultiplied format, with no changes, still the destinationOut does not what I expect.

    Thanks for help in advance!!!
    Olli

  5. #5
    Join Date
    Feb 2009
    Posts
    79
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPainter::setCompositingMode without effect

    Hmm,

    I ahve found a workaround:

    I just use another temporary QImage where I render the image together with the cutmask. This image is then rendered into the actual painter given by the paint(...) -method:

    Qt Code:
    1. void GraphicsHairstyleItem::paint(QPainter *painter,
    2. const QStyleOptionGraphicsItem *option, QWidget *widget)
    3. {
    4. Q_UNUSED(option);
    5. Q_UNUSED(widget);
    6.  
    7.  
    8.  
    9. QImage toPaint (m_img_warped.width(), m_img_warped.height(), m_img_warped.format());
    10. QPainter p(&toPaint);
    11. p.setCompositionMode(QPainter::CompositionMode_Source);
    12. p.fillRect(m_imgrect, Qt::transparent);
    13. p.setCompositionMode(QPainter::CompositionMode_SourceOver);
    14. p.drawImage(0, 0, m_img_warped);
    15. p.setCompositionMode(QPainter::CompositionMode_DestinationOut);
    16. p.drawImage(0, 0, m_img_cutmask);
    17. p.end();
    18.  
    19.  
    20. painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
    21. painter->drawImage(m_imgrect, toPaint);
    22. }
    To copy to clipboard, switch view to plain text mode 

    This does what it should.

    But this is not elegant. I cannot understand why it should not be possible to do this DestinationOut rendering dierctly in the event-painter.

    Can anyone clarify my problem?

    Thnaks a lot in advance,
    Olli

Similar Threads

  1. QPushButton fade effect without animation
    By Kostanev in forum Qt Programming
    Replies: 11
    Last Post: 12th November 2008, 09:46
  2. Screen Resolution stting takes effect on application !
    By santhoshv84 in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2008, 13:06
  3. QFont::setPointSize() does not take effect
    By lovelypp in forum Qt Programming
    Replies: 5
    Last Post: 14th July 2008, 19:44
  4. QImage glow effect
    By MarkoSan in forum Qt Programming
    Replies: 4
    Last Post: 31st March 2008, 06:15
  5. Designing a "slide-out" effect
    By Gray in forum Qt Programming
    Replies: 1
    Last Post: 26th September 2007, 10:52

Tags for this Thread

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.