PDA

View Full Version : QGraphicsView/Scene and QTransform



rubenvb
24th December 2009, 12:04
Hey guys,

I have been trying to get the QGraphicsView::setTransform( const QTransform & ) to work. I have (simplified):


class QPlot : public QGraphicsView
{
...
protected:
resizeEvent( QResizeEvent *event );
private:
QGraphicsScene *scene;
}

with implementation:

QPlot::QPlot( QWidget *parent )
: QGraphicsView( parent ),
scene( new QGraphicsScene() )
{
// prepare
//setRenderHints( QPainter::Antialiasing );
setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
scene->setSceneRect( 0.0, 0.0, 100.0, 100.0 );

// initial transformation
QTransform transform;
QRectF worldRect( QPointF(0.,0.), QPointF(100.,100.) );
QRectF sceneRect( QPointF(0.,0.), QPointF(100., 100.) );
QTransform::quadToQuad(QPolygon(worldRect), QPolygon(sceneRect), transform);

setSceneRect( sceneRect );
setTransform( transform );

// x-grid
scene->addLine( 100.0, 0.0, 0.0, 0.0 );
scene->addLine( 100.0, 20.0, 0.0, 20.0 );
scene->addLine( 100.0, 40.0, 0.0, 40.0 );
scene->addLine( 100.0, 60.0, 0.0, 60.0 );
scene->addLine( 100.0, 80.0, 0.0, 80.0 );
scene->addLine( 100.0, 100.0, 0.0, 100.0 );
// y-grid
scene->addLine( 0.0, 0.0, 0.0, 100.0 );
scene->addLine( 20.0, 0.0, 20.0, 100.0 );
scene->addLine( 40.0, 0.0, 40.0, 100.0 );
scene->addLine( 60.0, 0.0, 60.0, 100.0 );
scene->addLine( 80.0, 0.0, 80.0, 100.0 );
scene->addLine( 100.0, 0.0, 100.0, 100.0 );
// QPLotScale transform check
setScene( scene );
}
void QPlot::resizeEvent( QResizeEvent *event )
{
fitInView( scene->sceneRect(), Qt::IgnoreAspectRatio );


qDebug() << transform().m11() << transform().m12() << transform().m13() << endl
<< transform().m21() << transform().m22() << transform().m23() << endl
<< transform().m31() << transform().m32() << transform().m33() << endl;
}

All I get is a FULL grid that stretches to fit the window size. This is okay, but...
I would like to have the grid (ten lines) to be transformed by the QTransform. I have searched the web for an example, but they always implement the transformation directly on the QPainter, not via the QGraphicsView :(
What I want is to have eg only a part of the grid to be shown (only the center squares, only the topleft corner, stuff like that). I have tried to ask on these forums before, but always without answer :(

Any help is appreciated!