Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Problems capturing QGraphicsView in QImage
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Problems capturing QGraphicsView in QImage

Scheduled Pinned Locked Moved General and Desktop
3 Posts 1 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sipickles
    wrote on last edited by
    #1

    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);
    _mapView->setScene(_mockMapModel);
    }
    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);
    _mockMapModel->addItem(item);
    _mockMapModel->addItem(item2);

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

    QImage image(_mapView->sceneRect().size().toSize(), QImage::Format_ARGB32);
    image.fill(Qt::blue);
    QPainter painter(&image);
    painter.setRenderHint(QPainter::Antialiasing);
    _mapView->render(&painter);
    image.save("testSimpleScene2.png");
    }
    @

    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

    Thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sipickles
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sipickles
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved