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

varun singh mailthatmale at yahoo.co.in
Tue Aug 4 12:42:07 CEST 2009


hi,
  Well thanks John, the application is working now.
  And sorry people for sending the mail twice in a row (by mistake).

-Varun





Date: Fri, 31 Jul 2009 14:05:12 -0400
From: John McClurkin <jwm at nei.nih.gov>
Subject: Re: [Qt-interest] How to paint the Background of QImage
    without loosing its Foreground Contents.
To: qt-interest at trolltech.com
Message-ID: <h4vboo$pcv$1 at eple.troll.no>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

varun singh wrote:
> hi,
> 
>   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.
> 

Well, your code looks good so the most obvious explanation is that the 
QColor that is created by your call
    QColor(mPaintImage->pixel(x, y))
is different from mFColor, even when the pixel in mPaintImage was 
colored by mFColor. You are probably going to have to single step this 
in a debugger and check values, possibly even (God forbid) dump the 
mPaintImage pixel values to a text file. You might try something like:

for(y = 0; y <= mPaintImage->height() - 1; y++)
{
     pColor = QColor(mPaintImage->pixel(x, y);
     if(pColor != mFColor)
     {
    mPaintImage->setPixel(x, y, mBColor);
     }
}

Putting a break point on the if statement would let you examine both 
pColor and mFColor. Grueling work. Glad I don't have to do it.


------------------------------

Message: 4
Date: Fri, 31 Jul 2009 14:06:37 -0400
From: John McClurkin <jwm at nei.nih.gov>
Subject: Re: [Qt-interest] How to paint the Background of QImage
    without loosing its Foreground Contents.
To: qt-interest at trolltech.com
Message-ID: <h4vbrd$pcv$2 at eple.troll.no>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

varun singh wrote:
> hi,
> 
>   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

P.S. maybe the alpha values are different.


      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