Results 1 to 9 of 9

Thread: QPainter::drawPixmap with floats ?

  1. #1
    Join Date
    Oct 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QPainter::drawPixmap with floats ?

    I recently discovered that:
    - either QPainter::drawPixmap, despite the fact that its API accepts QPointF/QRectF, doesn't handle floating point precision.
    - or I'm doing something wrong.

    Below, you'll find an illusation of the problem.

    Qt Code:
    1. #include <QApplication>
    2. #include <QGraphicsScene>
    3. #include <QGraphicsView>
    4. #include <QGraphicsRectItem>
    5. #include <QPainter>
    6.  
    7. class MyGraphicsItem : public QGraphicsRectItem
    8. {
    9. public:
    10.  
    11. MyGraphicsItem()
    12. {
    13. setRect(0,0,10,10);
    14. }
    15.  
    16. void paint ( QPainter * painter, const QStyleOptionGraphicsItem * , QWidget * )
    17. {
    18. //Draw a grid of horizontal and vertical stripes.
    19. //Each stripe has a thickness of 1 QPainter unit.
    20. painter->setPen(Qt::NoPen);
    21. painter->setBrush( QColor( 0,255,0,127 ) );
    22. for ( int i = 0 ; i < 10 ; i+=2 )
    23. painter->drawRect( QRect(0,i,10,1) );
    24.  
    25. painter->setBrush( QColor( 255,0,0,127 ) );
    26. for ( int i = 0 ; i < 10 ; i+=2 )
    27. painter->drawRect( QRect(i,0,1,10) );
    28.  
    29. //Build a colored QPixmap.
    30. QPixmap p(5,2);
    31. p.fill( QColor(0,0,255,127) );
    32.  
    33. //Build a QRectF.
    34. QRectF r( 1.4f,1.4f,5,5 );
    35. //Draw my QPixmap, fitting it in the QRectF.
    36. painter->drawPixmap( r , p , QRectF( 0,0,p.width(),p.height() ) );
    37. //Problem: it behaves as:
    38. //painter->drawPixmap( r.toRect() , p , QRectF( 0,0,p.width(),p.height() ) );
    39. /*
    40. // Hack to get the expected behaviour.
    41. QRectF r1(r.x() * 10 , r.y() * 10 , r.width() * 10 , r.height() * 10 );
    42. painter->scale(0.1f,0.1f);
    43. painter->drawPixmap( r1 , p , QRectF( 0,0,p.width(),p.height() ) );
    44. painter->scale(10,10);
    45. */
    46. }
    47.  
    48. };
    49.  
    50. int main(int argc, char *argv[])
    51. {
    52. QApplication app(argc, argv);
    53.  
    54. QGraphicsScene * scene = new QGraphicsScene();
    55. QGraphicsView v(scene);
    56.  
    57. MyGraphicsItem* i = new MyGraphicsItem();
    58. i->scale(30,30);
    59. scene->addItem( i );
    60.  
    61. v.show();
    62.  
    63. return app.exec();
    64. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for any help !
    Last edited by christophe.daudin; 19th October 2009 at 11:16. Reason: be more polite

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

    Default Re: QPainter::drawPixmap with floats ?

    hmm... can you post a screen shot how it looks with:
    Qt Code:
    1. painter->drawPixmap( r , p , QRectF( 0,0,p.width(),p.height() ) );
    To copy to clipboard, switch view to plain text mode 

    and with the workaround?

    it should not matter, but can you also try using all params as floats where QRectF is being used? (i.e QRectF( 0.0,0.0,p.width(),p.height() ) etc)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPainter::drawPixmap with floats ?

    Expected behaviour (with workaround code):

    Observed behaviour:


    And all-floats stuff doesn't work - well, that would have been surprising, anyway.

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

    Default Re: QPainter::drawPixmap with floats ?

    no attachment is visible...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Oct 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPainter::drawPixmap with floats ?

    Yes, I don't know why. I put an IMG tag with an imageshack link. Well, "Right click" -> "Open image in new tab" still works ok, right ?

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

    Default Re: QPainter::drawPixmap with floats ?

    there is no link in the post to anything!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Oct 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPainter::drawPixmap with floats ?


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

    Default Re: QPainter::drawPixmap with floats ?

    Hmm... indeed interesting.
    I for one don't see anything wrong with your code, so it might really be a bug.
    Or some of the experts here know more about this.

    I suggest you register it! - but before have a look maybe it is already a known issue.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Oct 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPainter::drawPixmap with floats ?

    That's the plan. Wait some days here for some experts. And submit it if necessary.
    (there's nothing like that in the Qt bug tracker)

Similar Threads

  1. QPainter::drawPixmap always produces black on windows?
    By spbots in forum Qt Programming
    Replies: 0
    Last Post: 18th September 2009, 16:42

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
  •  
Qt is a trademark of The Qt Company.