Congratulations to our 2022 Qt Champions!

[SOLVED] Resize QGraphicsItem when window resizes

  • Hello all!

    I am trying to resize a QGraphicsItem when I resize the app window but its not working.
    How could I do that?
    //Tool Box 3
    QGridLayout *gridLayoutInvestimento5 = new QGridLayout(boxInvestimento3);
    QGraphicsView *graphicsViewFluxoCaixa = new QGraphicsView(boxInvestimento3);
    QGraphicsScene *graphicsSceneFluxoCaixa = new QGraphicsScene(graphicsViewFluxoCaixa);

    //graphicsViewFluxoCaixa->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    //graphicsViewFluxoCaixa->fitInView(graphicsSceneFluxoCaixa->itemsBoundingRect(), Qt::IgnoreAspectRatio);
    QPen caneta(Qt::black);
    QGraphicsLineItem *linhaTempo = new QGraphicsLineItem(0,0,graphicsViewFluxoCaixa->width(),0);


    Thanks a lot.

  • The GraphicsView framework separates the scene composition from the view(s) where it is displayed. So you try to work against a basic design principle, which is always hard.

    Not impossible, of course.

    First, you have to register changes to the QGraphicsView. You can do this by subclassing QGraphicsView and handling the resizeEvent.
    Now, you basically have two options:

    1. Maybe all you want is zoom and scroll so the item is e.g. centered in the view (you were not very specific about what you want to do). In this case, all you need is to use QGraphicsView's scroll and zoom methods to get what you want (e.g. fitInView)
    2. If for some reason you really need to resize the actual QGraphicsItem, you can do it from there. But I need to point out that this has big disadvantages - think about what would happen if you had two QGraphicsViews connected to the same scene!

  • Thank you very much.

    I subclassed QGRaphicsView, and its is working.

Log in to reply