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