Important: Please read the Qt Code of Conduct -

QGraphicsView layout problem

  • Dear All:

    I am trying to create a QWidget that has a Custom made TitleBar and QGraphicsView, in the same way that QCreator does for the code editor. What I have done is to create a QWidget with a QHBoxLayou where I add the TitleBar and the QGraphicsView.

    So far everything is Ok except that I need the QGraphicsScene to fit the size of the QGraphicsView. What I did in the past when I work with the QGraphicsView alone without the layout was to override the resizeEvent and take the size of the QGraphicsView there and set it to the scene.

    The problem is that with the new QHBoxLayout this method is not being call and I don't understand why. If someone has any idea it will be very welcome.

    All the best

  • Lifetime Qt Champion


    Can you show a minimal code sample of what doesn't work ?

  • This is the class that contain both the Bar on top and the Graphics View

    TraceAverageEegView::TraceAverageEegView(QWidget *parent) :
    // Graphics View
    traceChannelSubView_ = new QGraphicsView(this);
    traceChannelSubView_->setAlignment(Qt::AlignLeft | Qt::AlignTop);

    // Top Bar
    informationBar_ = new TraceAverageEegInformationBar(this);

    QVBoxLayout *layout = new QVBoxLayout(this);


    // The class that implement the GraphicsView
    TraceAverageEegSubView::TraceAverageEegSubView(QWidget *parent) :

    void TraceAverageEegSubView::resizeEvent(QResizeEvent resizeEvent)
    scene = static_cast<TraceAverageEegScene*>(this->scene());

    QSize eventSize = resizeEvent->size();
    int left, right, top, bottom;
    this->getContentsMargins(&left, &top, &right, &bottom);
    QRect minimumRectangle(QPoint(left, top),
                           QSize((eventSize.width() - left - right),
                                 (eventSize.height() - top - bottom)));


    The resizeEvent is never call when I resize the TraceAverageEegView.


  • Lifetime Qt Champion

    Wouldn't fitInView do the job ?

  • Yes that will do the job if the resizeEvent of the QGraphicsView will be called but that is not the case. The question I have is the following:

    When a Widget with a layout change the size I guess that the layout resize all of the component that are inside? Then why the resizeEvent of the QGraphicsView is not call?

  • Lifetime Qt Champion

    void TraceAverageEegSubView::resizeEvent(QResizeEvent resizeEvent)

    Is it a typo in your pasted code or did you miss the * after QResizeEvent ?

Log in to reply