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



  • 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"



  • @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?


  • Moderators

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



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

    Thanks to all :)**


  • Moderators

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.