May 13, 2011

TanEk TanEk
Lab Rat
1 posts

Drawing a QLinearGradient in a QImage

 

Hi,

I am trying to render a gradient into an image to save it in a file. I am doing this:

  1.        
  2.         const int histogramSize = 256;
  3.         QLinearGradient gradient(0, 0, histogramSize, 0);
  4.         gradient.setStops(stops);
  5.  
  6.         QImage lut = QImage(QSize(1, histogramSize), QImage::Format_ARGB32);
  7.         QPainter p(&lut);
  8.         p.setCompositionMode(QPainter::CompositionMode_Source);
  9.         p.fillRect(0, 0, histogramSize, 1, gradient);
  10.  
  11.         const uchar * buffer = lut.bits();
  12.         const int nbOfBytes = histogramSize * 4 ;
  13.         assert(nbOfBytes == lut.byteCount());
  14.         const uchar * endBuffer = buffer + nbOfBytes;
  15.         QColor begin(lut.pixel(QPoint(0, 0)));
  16.         QColor end(lut.pixel(QPoint(0, histogramSize - 1)));
  17.         for(int i = 0 ; i < 2 ; ++i)
  18.         {
  19.             QColor color = stops[i].second;
  20.             std::cout << i << ": " << color.red() << ' ' << color.green() << ' ' << color.blue() << ' ' << color.alpha() << '\n';
  21.         }
  22.         std::cout << "begin: " << begin.red() << ' ' << begin.green() << ' ' << begin.blue() << ' ' << begin.alpha() << '\n';
  23.         std::cout << "end: " << end.red() << ' ' << end.green() << ' ' << end.blue() << ' ' << end.alpha() << '\n';

And the gradient is defined by two points like you can see in the screenshot

http://s2.postimage.org/7uipuh5it/Screen_Hunter_01_May_13_16_15.jpg

The result of the program is:

0: 252 252 252 2
1: 252 252 252 252
begin: 255 255 255 255
end: 205 205 205 255

However as you can see begin doesn’t have the alpha value I am expecting of them. I tried to change the RGB components and they change properly like I expect them to be but the alpha component never change. It is almost like if the gradient didn’t care on changing the alpha component even if I set the composition mode to source which should basically just dump the gradient into the image. Is it a bug or is it the normal behavior and if it is the normal behavior could someone explain me why?

Thanks

2 replies

December 11, 2014

YuriyRusinov YuriyRusinov
Lab Rat
1 posts

Could you update reference to screenshot ? When I try to see, I receive an error “Image not available”

December 17, 2014

Andre Andre
Robot Herder
6422 posts

The original post was over three years old, by a user who only made this one post. Please don’t try to revive the dead…

 
  ‹‹ QT-Creator’s debugger "just stopped" showing QString values      [Another Question2] How to hide a label with fade/opacity levels? ››

You must log in to post a reply. Not a member yet? Register here!