Qt World Summit: Register Today!

[SOLVED] Problems capturing QGraphicsView in QImage

  • Hi all, I'm new! Hooray!

    I am trying to capture a QGraphicsView widget as part of my testing process. In my test case for the overridden QGraphicsView I add some mock scene data and render to a QImage. However only part of the image is captured. I am pretty new to Qt so I think I must be doing something wrong although I have been looking online for days.

    Here's my code:

    @// .............

    void MapViewTest::initTestCase()
    _mockMapModel = new QGraphicsScene( -100.0, -100.0, 100.0, 100.0 );
    _mapView = new MapView(_parent);
    void MapViewTest::cleanupTestCase()
    delete _mapView;
    delete _mockMapModel;

    void MapViewTest::testSimpleScene()
    QGraphicsEllipseItem *item = new QGraphicsEllipseItem(0,0, 100, 100);
    QGraphicsEllipseItem *item2 = new QGraphicsEllipseItem(0, 0, 50, 50);

    _mapView->setRenderHints( QPainter::Antialiasing );
    _mapView->setSceneRect( 0.0, 0.0, 100.0, 100.0 );

    QImage image(_mapView->sceneRect().size().toSize(), QImage::Format_ARGB32);
    QPainter painter(&image);

    The image is saved but looks like this:
    <img src="http://ScrnSht.com/eqbwbg">

    I have ellipses but the image is cut off below perhaps 50 pixels.

    Can anyone explain why? Its like the elipses havent finished rendering


  • Sorry I should explain that MapView is inheriting QGraphicsView, with no extra functionality or overridden functions (yet).

  • It turns out the line

    @ _mapView = new MapView(_parent);

    was clipping the view. By setting the parent to NULL in my test cases, I got the whole view saved in the QImage.

Log in to reply