Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Zoom some items to extent in QGraphicsView?



  • I have an QGraphicsView. In this view I have some graphics items.
    I'd like to zoom these items to extent in the view.

    How can I solve this?


  • Moderators

    Use "QGraphicsView::scale()":http://qt-project.org/doc/qt-4.8/qgraphicsview.html#scale Invoke it with same factor for x and y to get uniform zoom.



  • I've tried this:
    @ fitInView(scene->sceneRect(), Qt::IgnoreAspectRatio); @
    and this:
    @ scale(scene->sceneRect().x(), scene->sceneRect().y()); @
    and this:
    @ scale(1.0, 1.0); @

    None of them is working properly...


  • Moderators

    Try
    @
    scale (0.8, 0.8);
    @
    then.



  • [quote author="sierdzio" date="1354626126"]Try
    @
    scale (0.8, 0.8);
    @
    then.[/quote]

    This is not exactly what I want.
    I need a rectangle (wich is contains all of my graphicsitems). This rectangle has a dimension. I'd like to zoom my view to this rectangle (extent).
    But I don't know how...



  • I've tried this:
    @ fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio); @

    And when program is running I got this:
    QTransform::scale with NaN called

    scene->itemsBoundingRect() gives me : rect: 514 / 20 : nan / nan (x, y, width, height)

    Why NaN the width and height???


  • Moderators

    There is probably something wrong elsewhere if boundingrect has negative size.

    NaN == Not a Number, but you are probably not asking about that.



  • Thanks.
    I updated my last post. See please.
    Anyway my scene and view is: 0, 0, 600, 400


  • Moderators

    I left QGraphicsView for QML, so all I can do is to try recalling things from dusted memory :)

    Lack of width/height might come from lacking information in children items. Make sure you set properties as needed. If you have some item subclasses, make sure the size methods that need to be reimplemented return valid sizes.

    Or, of course, try doing some further debugging. Print x, y, width and height of all the items an inspect - maybe that would show you what is missing.


Log in to reply