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

Issue with QChart layout when resizing the parent QChartView (Qt 5.14.0)



  • There is something wrong around the QChart layouts.

    Its containing QChartView receive a resize event but the chart stays in the old size. The only way to have it refreshed is when a QGraphicsItem (QGraphicsSimpleTextItem) that I added myself in the scene() along with the chart gets updated. This is not a viable solution because the chart stays in its old size for as long as the user doesn't interact with the chart.

    On my first attempt to fix that, I did try to override the QChartView::resizeEvent() method by doing this:

    void LinkedChartView::resizeEvent(QResizeEvent *event)
    {
        QChartView::resizeEvent(event);
        chart()->update();
    }
    

    to my great surprise calling update() on a QChart object does nothing. This is either very counter-intuitive or maybe it should do something and it doesn't for the same bug that make the chart not resize in the first place.

    Finally, the workaround that works is:

    void LinkedChartView::resizeEvent(QResizeEvent *event)
    {
        QChartView::resizeEvent(event);
        chart()->layout()->invalidate();
        chart()->layout()->activate();
    }
    

    Again, I suspect that if the code was working as designed my workaround wouldn't be neccessary....



  • Here is some more context.

    I have 3 charts containing QLineSeries and some QScatterSeries, a QValueAxis vertically and a QDateTimeAxis horizontally.

    Those 3 charts view are placed into a QVBoxLayout.

    The chart view visibility is toggled on and off with the menu described in
    https://forum.qt.io/topic/110369/why-is-qt-makes-it-hard-to-activate-a-qaction-with-menu-from-qmenu

    The sure fire way to trigger the problem is hide and show the middle chart.

    The top most chart gets resized fine. I suspect it is because when the menu disappear that overlap a little bit the chart on its upper left corner, the chart is marked as dirty and needs to be repainted anyway.

    It is the chart at the bottom that needs my workaround to be resized properly.


Log in to reply