Unsolved 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 (http://doc.qt.io/qt-5/qtcharts-zoomlinechart-example.html), 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 theQChartView
and it works fine.
but it seems that the default scrollbar policy isScrollBarAlwaysOff
.
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 (http://doc.qt.io/qt-5/qtcharts-zoomlinechart-chartview-h.html), 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" QT_BEGIN_NAMESPACE class Ui_MainWindow { public: QWidget *centralWidget; ZoomableView *graphicsView; QMenuBar *menuBar; QToolBar *mainToolBar; QStatusBar *statusBar; void setupUi(QMainWindow *MainWindow) { if (MainWindow->objectName().isEmpty()) MainWindow->setObjectName(QStringLiteral("MainWindow")); MainWindow->resize(1076, 802); centralWidget = new QWidget(MainWindow); centralWidget->setObjectName(QStringLiteral("centralWidget")); graphicsView = new ZoomableView(centralWidget); graphicsView->setObjectName(QStringLiteral("graphicsView")); graphicsView->setGeometry(QRect(10, 10, 1051, 721)); graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); graphicsView->setSizeAdjustPolicy(QAbstractScrollArea::AdjustIgnored); graphicsView->setSceneRect(QRectF(0, 0, 0, 0)); MainWindow->setCentralWidget(centralWidget); menuBar = new QMenuBar(MainWindow); menuBar->setObjectName(QStringLiteral("menuBar")); menuBar->setGeometry(QRect(0, 0, 1076, 20)); MainWindow->setMenuBar(menuBar); mainToolBar = new QToolBar(MainWindow); mainToolBar->setObjectName(QStringLiteral("mainToolBar")); MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar); statusBar = new QStatusBar(MainWindow); statusBar->setObjectName(QStringLiteral("statusBar")); MainWindow->setStatusBar(statusBar); retranslateUi(MainWindow); QMetaObject::connectSlotsByName(MainWindow); } // setupUi void retranslateUi(QMainWindow *MainWindow) { MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0)); } // retranslateUi }; namespace Ui { class MainWindow: public Ui_MainWindow {}; } // namespace Ui QT_END_NAMESPACE #endif // UI_MAINWINDOW_H
-
@vorlket
do you removeq_ptr->setScene
inQChartViewPrivate
's constructor?
I wonder why you don't create view class by inheritingQChart
but to modifyQChart
'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.