#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QPainter>
{
public:
MyGraphicsItem()
{
setRect(0,0,10,10);
}
{
//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.
p.
fill( QColor(0,
0,
255,
127) );
//Build a QRectF.
//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[])
{
MyGraphicsItem* i = new MyGraphicsItem();
i->scale(30,30);
scene->addItem( i );
v.show();
return app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks