PDA

View Full Version : QPainter::drawPixmap with floats ?



christophe.daudin
19th October 2009, 12:12
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.



#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QPainter>

class MyGraphicsItem : public QGraphicsRectItem
{
public:

MyGraphicsItem()
{
setRect(0,0,10,10);
}

void paint ( QPainter * painter, const QStyleOptionGraphicsItem * , QWidget * )
{
//Draw a grid of horizontal and vertical stripes.
//Each stripe has a thickness of 1 QPainter unit.
painter->setPen(Qt::NoPen);
painter->setBrush( QColor( 0,255,0,127 ) );
for ( int i = 0 ; i < 10 ; i+=2 )
painter->drawRect( QRect(0,i,10,1) );

painter->setBrush( QColor( 255,0,0,127 ) );
for ( int i = 0 ; i < 10 ; i+=2 )
painter->drawRect( QRect(i,0,1,10) );

//Build a colored QPixmap.
QPixmap p(5,2);
p.fill( QColor(0,0,255,127) );

//Build a QRectF.
QRectF r( 1.4f,1.4f,5,5 );
//Draw my QPixmap, fitting it in the QRectF.
painter->drawPixmap( r , p , QRectF( 0,0,p.width(),p.height() ) );
//Problem: it behaves as:
//painter->drawPixmap( r.toRect() , p , QRectF( 0,0,p.width(),p.height() ) );
/*
// Hack to get the expected behaviour.
QRectF r1(r.x() * 10 , r.y() * 10 , r.width() * 10 , r.height() * 10 );
painter->scale(0.1f,0.1f);
painter->drawPixmap( r1 , p , QRectF( 0,0,p.width(),p.height() ) );
painter->scale(10,10);
*/
}

};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QGraphicsScene * scene = new QGraphicsScene();
QGraphicsView v(scene);

MyGraphicsItem* i = new MyGraphicsItem();
i->scale(30,30);
scene->addItem( i );

v.show();

return app.exec();
}


Thanks for any help !

high_flyer
19th October 2009, 13:02
hmm... can you post a screen shot how it looks with:

painter->drawPixmap( r , p , QRectF( 0,0,p.width(),p.height() ) );


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)

christophe.daudin
19th October 2009, 13:19
Expected behaviour (with workaround code):
http://img23.imageshack.us/i/workaround.png/
Observed behaviour:
http://img23.imageshack.us/i/bughv.png/

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

high_flyer
19th October 2009, 15:56
no attachment is visible... :confused:

christophe.daudin
19th October 2009, 16:42
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 ?

high_flyer
20th October 2009, 10:26
there is no link in the post to anything!

christophe.daudin
20th October 2009, 10:53
Workaround. (http://img23.imageshack.us/i/workaround.png/)
Bug. (http://img23.imageshack.us/i/bughv.png/)

high_flyer
20th October 2009, 11:15
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.

christophe.daudin
20th October 2009, 11:19
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)