+ Reply to Thread
Results 1 to 9 of 9

Thread: QPainter: painted color (brush) and pixel value (read after the draw) are different?

  1. #1
    Join Date
    Feb 2017
    Posts
    4
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Question QPainter: painted color (brush) and pixel value (read after the draw) are different?

    Hi all,
    I have a problem with QPainter. (Qt 5.4)

    I have an Image in Format_ARGB32
    I want to print a given Rgba (uint) value on to this using QPainter draw function and later fetch the value using image.pixel(x,y).
    But the value painted and the value got from the pixel are different !!. What could be the problem.
    KIndly help.

    The sample code is like this.
    ..............
    QImage image(100, 100, QImage::Format_ARGB32);
    uint value = 0x44fa112b; //some value..
    QPainter painter(&image);
    painter.setCompositionMode(QPainter::CompositionMo de_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());

    uint value1 = image.pixel(50,50);


    // value1 IS NOT EQUAL TO value. Why??

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,794
    Thanks
    3
    Thanked 436 Times in 419 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ

    Did you test this sample code ? What value you got back ?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Feb 2017
    Posts
    4
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ

    Yes. I have tried (not exactly this one). But most of the time there is one bit difference in red value, green value or both the values. I have tried with different renderHints also. Does not make any difference.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,355
    Thanks
    21
    Thanked 379 Times in 373 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ

    do the values only differ in one bit?
    I am surprised they are at all close!
    QPainter::drawRect() does not fill the rectangle, so position 50,50 (middle of your rect) must be different than the outline which QPainter drew with drawRect() - or am I missing something in your post?
    Last edited by high_flyer; 18th February 2017 at 19:48.
    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
    Watch these videos, and read his books they are must if you want to write clean code:
    https://sites.google.com/site/unclebobconsultingllc/

    Recommended off-line reading Qt:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    2,936
    Thanks
    184
    Thanked 459 Times in 450 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ

    QPainter::draw() does not fill the rectangle
    There is no draw() method in QPainter. And if you set a QBrush, it uses it in the drawRect() method to fill the rectangle. (And the current QPen, if any, to draw the outline).

    (not exactly this one)
    Meaning that the code in the post is probably not the actual code exhibiting the problem, so who knows what else might be going on?
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Feb 2017
    Posts
    4
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ

    Thank you all..
    Posting the actual code with result..

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QDebug>
    4. #include <QPainter>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11.  
    12. QImage image(100, 100, QImage::Format_ARGB32);
    13. image.fill(0);
    14. uint value = 0x44fa112b; //some value..
    15. QPainter painter(&image);
    16. painter.setCompositionMode(QPainter::CompositionMode_Source);
    17. QColor color(qRed(value), qGreen(value), qBlue(value), qAlpha(value));
    18. QBrush brush(color);
    19. painter.setBrush(brush);
    20. painter.drawRect(0,0,image.width(), image.height());
    21.  
    22. uint value1 = image.pixel(50,50);
    23.  
    24. qDebug() << "RESULT : value = " << QString::number(value,16) << "value1 = " << QString::number(value1,16);
    25. }
    26.  
    27. MainWindow::~MainWindow()
    28. {
    29. delete ui;
    30. }
    To copy to clipboard, switch view to plain text mode 

    And the result is......
    RESULT : value = "44fa112b" value1 = "44fb1329"

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,068
    Thanks
    35
    Thanked 1,457 Times in 1,412 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ

    If you want to fill the image with a specific color, there is QImage::fill().

    Also:

    * have you verified the QColor value? Is the rgba() result the same as the input
    * if not, have you tried constructing the QColor directly from the value?

    Cheers,
    _

  8. #8
    Join Date
    Feb 2017
    Posts
    4
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ

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

    Thanks to all

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,355
    Thanks
    21
    Thanked 379 Times in 373 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QPainter: painted color (brush) and pixel value (read after the draw) are differ

    There is no draw() method in QPainter. And if you set a QBrush, it uses it in the drawRect() method to fill the rectangle. (And the current QPen, if any, to draw the outline).
    the draw() was a typo, thanks.
    Ah, and indeed, in the docs it says drawRect() will use the brush as well.
    Last edited by high_flyer; 18th February 2017 at 20:20.
    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
    Watch these videos, and read his books they are must if you want to write clean code:
    https://sites.google.com/site/unclebobconsultingllc/

    Recommended off-line reading Qt:
    The Foundations of Qt Development (ISBN: 1-59059-831-8)

+ Reply to Thread

Similar Threads

  1. QPainter above already painted
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 26th February 2014, 19:59
  2. How can I draw with brush on QLabel?
    By suseway in forum Qt Programming
    Replies: 4
    Last Post: 25th October 2010, 21:13
  3. How to read a color of a pixel on a Widget?
    By wringer in forum Newbie
    Replies: 5
    Last Post: 4th December 2009, 06:31
  4. Replies: 5
    Last Post: 20th October 2009, 07:18
  5. How to get pixel color from QPainter ??
    By rameshg87 in forum Qt Programming
    Replies: 1
    Last Post: 10th August 2008, 08:58

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts