[Qt-interest] How to paint the Background of QImage without loosing its Foreground Contents.

varun singh mailthatmale at yahoo.co.in
Tue Aug 4 14:52:14 CEST 2009


hi,

Well though the problem is solved, but i would try this one.
Thanks

-Varun


--- On Tue, 4/8/09, Samuel Rødal <sroedal at trolltech.com> wrote:

> From: Samuel Rødal <sroedal at trolltech.com>
> Subject: Re: [Qt-interest] How to paint the Background of QImage without loosing its Foreground Contents.
> To: "varun singh" <mailthatmale at yahoo.co.in>
> Cc: qt-interest at trolltech.com
> Date: Tuesday, 4 August, 2009, 4:37 PM
> >>   Well, i'm
> trying to make a painting application. I'm getting some
> trouble with setting the background color.
> >> 
> >>   I've QImage object as my painting
> space, hence i used QImage->fill() fuction to fill the
> QImage with a particular color.
> >> 
> >>   But, if i scribble something on
> QImage (with Pen) and then use fill function, it clears
> everything n fills the image with the new color (well,
> that's obvious)...
> >> 
> >>   Hence I've run nested for loops
> to access each pixel of Qimage and check whether it is
> already colored with Pen, if it is not then i call
> setPixel() function to color it with background color. 
> >>   But its not working, each pixel
> is still getting painted.
> >> 
> >> Here is the code
> >> 
> >>
> ******************************************************************************
> >> //mBColor is of type QRgb.
> >> void PaintWidget::setBColor(QRgb color) //sets the
> Background Color
> >> {
> >>     mBColor = color;
> >>     int x, y;
> >>     for(x = 0; x <=
> mPaintImage->width() - 1; x++)     {
> >>         for(y = 0; y
> <= mPaintImage->height() - 1; y++)
> >>         {
> >>         
>    if(QColor(mPaintImage->pixel(x, y)) !=
> mFColor) //mFColor is         
>    {           
>                
>                
>    //Foregournd       
>      
>    mPaintImage->setPixel(x, y,
> mBColor);       //Ccolor. 
>             }
> >>         }
> >>     }
> >>    emit bColorChanged(mBColor);
> >>    update();
> >> }
> >> 
> >>
> *******************************************************************************
> >> 
> >>   I'm Desperately looking for some
> help.
> >> 
> >> -Varun
> 
> Sounds like a better solution is two separate this into two
> layers, one foreground layer which is a QImage with
> Format_ARGB32_Premultiplied and a transparent background,
> and a background layer which is just a simple fill color.
> Then when you want to display the image first do a fill
> rect  the size of the image with the background color,
> and then draw the image on top with SourceOver compositing
> (the default QPainter composition mode).
> 
> --
> Samuel
> 


      See the Web&#39;s breaking stories, chosen by people like you. Check out Yahoo! Buzz. http://in.buzz.yahoo.com/




More information about the Qt-interest-old mailing list