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. Why does the read-back pixel color differ from color drawn on a QImage?
Forum Updated to NodeBB v4.3 + New Features

Why does the read-back pixel color differ from color drawn on a QImage?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 805 Views 3 Watching
  • 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.
  • R Offline
    R Offline
    rajeshma
    wrote on last edited by
    #1

    I have a problem with QPainter on QImage in Qt 5.4.

    The image has Format_ARGB32. I want to set a given RGBA value on pixels in the image using a QPainter draw function and later read the value back using QImage::pixel.

    Yet the value painted and the value read back are different. What am I doing wrong?

    Sample code:

    #include "mainwindow.h"
    #include <QApplication>
    #include <QDebug>
    #include <QPainter>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QImage image(100, 100, QImage::Format_ARGB32);
        image.fill(0);
        uint value = 0x44fa112b; //some value..
        QPainter painter(&image);
        painter.setCompositionMode(QPainter::CompositionMode_Source);
        QColor color(qRed(value), qGreen(value), qBlue(value), qAlpha(value));
        QBrush brush(color);
        painter.setBrush(brush);
        painter.drawRect(0,0,image.width(), image.height());
    
        painter.end();
    
        uint value1 = image.pixel(50,50);
    
        qDebug() << "RESULT : value = " << QString::number(value,16) << "value1 = " << QString::number(value1,16);
    
        return a.exec();
    }
    

    Result: RESULT : value = "44fa112b" value1 = "44fb1329"

    matthew.kuiashM 1 Reply Last reply
    0
    • R rajeshma

      I have a problem with QPainter on QImage in Qt 5.4.

      The image has Format_ARGB32. I want to set a given RGBA value on pixels in the image using a QPainter draw function and later read the value back using QImage::pixel.

      Yet the value painted and the value read back are different. What am I doing wrong?

      Sample code:

      #include "mainwindow.h"
      #include <QApplication>
      #include <QDebug>
      #include <QPainter>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QImage image(100, 100, QImage::Format_ARGB32);
          image.fill(0);
          uint value = 0x44fa112b; //some value..
          QPainter painter(&image);
          painter.setCompositionMode(QPainter::CompositionMode_Source);
          QColor color(qRed(value), qGreen(value), qBlue(value), qAlpha(value));
          QBrush brush(color);
          painter.setBrush(brush);
          painter.drawRect(0,0,image.width(), image.height());
      
          painter.end();
      
          uint value1 = image.pixel(50,50);
      
          qDebug() << "RESULT : value = " << QString::number(value,16) << "value1 = " << QString::number(value1,16);
      
          return a.exec();
      }
      

      Result: RESULT : value = "44fa112b" value1 = "44fb1329"

      matthew.kuiashM Offline
      matthew.kuiashM Offline
      matthew.kuiash
      wrote on last edited by
      #2

      @rajeshma Hi, I just tried this exact example with Qt 5.8 on macOS and I do not see the problem. Both value and value1 equal 0x 44fa112b.

      Is it possible to try this on a more up-to-date version of Qt e.g. 5.8?

      The legendary cellist Pablo Casals was asked why he continued to practice at age 90. "Because I think I'm making progress," he replied.

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Looks like a bug in 5.4. You should upgrade if you can.

        1 Reply Last reply
        1
        • R Offline
          R Offline
          rajeshma
          wrote on last edited by
          #4

          **Problem solved!!
          Working properly when I tried with Qt 5.8
          Looks like a bug with Qt 5.4.

          Thanks to all :)**

          ? 1 Reply Last reply
          0
          • R rajeshma

            **Problem solved!!
            Working properly when I tried with Qt 5.8
            Looks like a bug with Qt 5.4.

            Thanks to all :)**

            ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @rajeshma Next time, please mark your thread as solved, see: https://forum.qt.io/topic/71830 .

            1 Reply Last reply
            1

            • Login

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