#include <QtCore/QDebug>#include <QtGui/QApplication>#include <QtGui/QGraphicsItem>#include <QtGui/QGraphicsScene>#include <QtGui/QGraphicsView>class GraphicsItem : public QGraphicsItem{public: GraphicsItem() : QGraphicsItem() {} QRectF boundingRect() const { return QRectF( 0, 0, 100, 100 ); } void paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget ) { qDebug() << painter->worldTransform().inverted().mapRect( painter->viewport() ); qDebug() << ( (QGraphicsView*)widget->parent() )->mapToScene( painter->viewport() ).boundingRect(); }};int main( int argc, char** argv ){ QApplication app( argc, argv ); GraphicsItem* item = new GraphicsItem(); QGraphicsScene scene; scene.addItem( item ); scene.setSceneRect( item->boundingRect() ); QGraphicsView view; view.setScene( &scene ); app.setActiveWindow( &view ); view.show(); view.resize( 300, 100 ); view.fitInView( scene.sceneRect() ); return app.exec();}
QRect(-1,-2 101x104) QRectF(-0.680272,-2.12766 101.361x104.255)
#include <QtCore/QDebug>#include <QtGui/QApplication>#include <QtGui/QGraphicsItem>#include <QtGui/QGraphicsScene>#include <QtGui/QGraphicsView>class GraphicsItem : public QGraphicsItem{public: GraphicsItem() : QGraphicsItem() {} QRectF boundingRect() const { return QRectF( 0, 0, 100, 100 ); } void paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget ) { painter->drawRect( boundingRect() ); qDebug() << painter->worldTransform().inverted().mapRect( painter->viewport() ); qDebug() << ( (QGraphicsView*)widget->parent() )->mapToScene( painter->viewport() ).boundingRect(); }};int main( int argc, char** argv ){ QApplication app( argc, argv ); GraphicsItem* item = new GraphicsItem(); QGraphicsScene scene; scene.addItem( item ); scene.setSceneRect( item->boundingRect() ); QGraphicsView view; view.setScene( &scene ); app.setActiveWindow( &view ); view.show(); view.resize( 300, 300 ); view.fitInView( scene.sceneRect() ); return app.exec();}