Important: Please read the Qt Code of Conduct -

QAbstractScrollArea ScrollbarPolicy with a customized qchartview

  • To have a zoomable line chart, I created a view class by replicating the qchartview class in the zoomline example (, with the difference being removal of the qgraphicsscene variable. With this view class, I tried to generate the (horizontal) scrollbar via the scrollbar policy. This trick or method works with qgraphicsview but doesn't with the created view class. What would be the likely causes?

  • @vorlket
    what scrollbar policy you set ? maybe the chart is not big enough, so it does not show the scrollbar.

  • Doubt it. Tried both 'asneeded' and 'alwayson' where the latter generates scrollbar detached from the chart, i.e. it doesn't scroll with the chart.

  • @vorlket
    I have tried make a simple program using the QChartView and it works fine.
    but it seems that the default scrollbar policy is ScrollBarAlwaysOff.
    can you provide your code so that we can have more information.

  • The view class is based on the QChartView class with the following changes:

    In Qt/5.7/Src/qtcharts/src/charts/qchartview_p.h, removed line 66: QGraphicsScene *m_scene;

    In Qt/5.7/Src/qtcharts/src/charts/qchartview.cpp, replaced line 293: m_scene->removeItem(m_chart); with m_chart->scene()->removeItem(m_chart);

    In Qt/5.7/Src/qtcharts/src/charts/qchartview.cpp, removed line 295: m_scene->addItem(m_chart);

    With the changed view class I made the following change to the zoom line example:
    In chartview.h (, added constructor ChartView(QWidget *parent = 0);

    ui_mainwindow.h: graphicsView object is Graphics View widget promoted to the ChartView (ZoomableView in my code) in the zoom line example.

    #ifndef UI_MAINWINDOW_H
    #define UI_MAINWINDOW_H
    #include <QtCore/QVariant>
    #include <QtWidgets/QAction>
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QButtonGroup>
    #include <QtWidgets/QHeaderView>
    #include <QtWidgets/QMainWindow>
    #include <QtWidgets/QMenuBar>
    #include <QtWidgets/QStatusBar>
    #include <QtWidgets/QToolBar>
    #include <QtWidgets/QWidget>
    #include "zoomableview.h"
    class Ui_MainWindow
        QWidget *centralWidget;
        ZoomableView *graphicsView;
        QMenuBar *menuBar;
        QToolBar *mainToolBar;
        QStatusBar *statusBar;
        void setupUi(QMainWindow *MainWindow)
            if (MainWindow->objectName().isEmpty())
            MainWindow->resize(1076, 802);
            centralWidget = new QWidget(MainWindow);
            graphicsView = new ZoomableView(centralWidget);
            graphicsView->setGeometry(QRect(10, 10, 1051, 721));
            graphicsView->setSceneRect(QRectF(0, 0, 0, 0));
            menuBar = new QMenuBar(MainWindow);
            menuBar->setGeometry(QRect(0, 0, 1076, 20));
            mainToolBar = new QToolBar(MainWindow);
            MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
            statusBar = new QStatusBar(MainWindow);
        } // setupUi
        void retranslateUi(QMainWindow *MainWindow)
            MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0));
        } // retranslateUi
    namespace Ui {
        class MainWindow: public Ui_MainWindow {};
    } // namespace Ui
    #endif // UI_MAINWINDOW_H

  • @vorlket
    do you remove q_ptr->setScene in QChartViewPrivate's constructor?
    I wonder why you don't create view class by inheriting QChart but to modify QChart's source code.

  • The scrollbar doesn't seem to work with QchartView while does with Graphics View Framework, i.e. chart, scene, and view are decoupled.

Log in to reply