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. Drawing a QLinearGradient in a QImage
QtWS25 Last Chance

Drawing a QLinearGradient in a QImage

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.8k Views
  • 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.
  • T Offline
    T Offline
    TanEk
    wrote on last edited by
    #1

    Hi,

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

    @
    const int histogramSize = 256;
    QLinearGradient gradient(0, 0, histogramSize, 0);
    gradient.setStops(stops);

        QImage lut = QImage(QSize(1, histogramSize), QImage::Format_ARGB32);
        QPainter p(&lut);
        p.setCompositionMode(QPainter::CompositionMode_Source);
        p.fillRect(0, 0, histogramSize, 1, gradient);
    
        const uchar * buffer = lut.bits();
        const int nbOfBytes = histogramSize * 4 ;
        assert(nbOfBytes == lut.byteCount());
        const uchar * endBuffer = buffer + nbOfBytes;
        QColor begin(lut.pixel(QPoint(0, 0)));
        QColor end(lut.pixel(QPoint(0, histogramSize - 1)));
        for(int i = 0 ; i < 2 ; ++i)
        {
            QColor color = stops[i].second;
            std::cout << i << ": " << color.red() << ' ' << color.green() << ' ' << color.blue() << ' ' << color.alpha() << '\n';
        }
        std::cout << "begin: " << begin.red() << ' ' << begin.green() << ' ' << begin.blue() << ' ' << begin.alpha() << '\n';
        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:

    bq. 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

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      YuriyRusinov
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        YuriyRusinov
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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...

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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...

            1 Reply Last reply
            0

            • Login

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