Weird behaviour of QBarSeries
-
Dear all, i'm facing a weird behaviour of QBarSeries (well, actually i cannot understand if the cause lies in QBarSet, QBarSeries or QChart ).
This is my minimal code example:
from PySide6.QtWidgets import QApplication, QMainWindow, QSlider, QVBoxLayout, QWidget from PySide6 import QtCharts from PySide6.QtCore import Qt import numpy as np import sys class MainWindow(QMainWindow): def __init__(self): super().__init__() self.central_widget = QWidget() self.setCentralWidget(self.central_widget) self.layout = QVBoxLayout(self.central_widget) self.nPolsSlider = QSlider(Qt.Horizontal) self.nPolsSlider.setMinimum(1) self.nPolsSlider.setMaximum(10) self.nPolsSlider.setValue(5) self.nPolsSlider.valueChanged.connect(self.update_chart) self.polsBarset = QtCharts.QBarSet('zernike_bp') self.pols = np.random.normal(0, 1, self.nPolsSlider.value()) self.polsBarset.append(list(self.pols)) self.polsSeries = QtCharts.QBarSeries() self.polsSeries.append(self.polsBarset) self.polsSeries.setBarWidth(1) self.polsChart = QtCharts.QChart() self.polsChart.addSeries(self.polsSeries) self.polsChart.createDefaultAxes() self.polsChart.legend().hide() self.polsChart.axisX(self.polsSeries).setRange(str(0), str(self.nPolsSlider.value())) self.polsChartView = QtCharts.QChartView(self.polsChart) self.layout.addWidget(self.nPolsSlider) self.layout.addWidget(self.polsChartView) self.update_chart() def update_chart(self): self.pols = np.random.normal(0, 1, self.nPolsSlider.value()) self.polsSeries.remove(self.polsBarset) # remove old QBarSet self.polsBarset = QtCharts.QBarSet('zernike_bp') # Create new QBarSet self.polsBarset.append(list(self.pols)) self.polsSeries.append(self.polsBarset) self.polsChart.axisX(self.polsSeries).setRange(str(0), str(self.nPolsSlider.value())) app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())What happens is that the initial value of the slider (thus the initial dimension of the QBarSet) is alwais the maximum value the chart can show. It can shrink, but it cannot grow higher.
Can you guess what i'm doing wrong?
moreover i noticed a deprecation warning
DeprecationWarning: Function: 'QChart.axisX(QAbstractSeries * series) const' is marked as deprecatedWhat is the correct code?
Thanks you all for your precious support
-
Dear all, i'm facing a weird behaviour of QBarSeries (well, actually i cannot understand if the cause lies in QBarSet, QBarSeries or QChart ).
This is my minimal code example:
from PySide6.QtWidgets import QApplication, QMainWindow, QSlider, QVBoxLayout, QWidget from PySide6 import QtCharts from PySide6.QtCore import Qt import numpy as np import sys class MainWindow(QMainWindow): def __init__(self): super().__init__() self.central_widget = QWidget() self.setCentralWidget(self.central_widget) self.layout = QVBoxLayout(self.central_widget) self.nPolsSlider = QSlider(Qt.Horizontal) self.nPolsSlider.setMinimum(1) self.nPolsSlider.setMaximum(10) self.nPolsSlider.setValue(5) self.nPolsSlider.valueChanged.connect(self.update_chart) self.polsBarset = QtCharts.QBarSet('zernike_bp') self.pols = np.random.normal(0, 1, self.nPolsSlider.value()) self.polsBarset.append(list(self.pols)) self.polsSeries = QtCharts.QBarSeries() self.polsSeries.append(self.polsBarset) self.polsSeries.setBarWidth(1) self.polsChart = QtCharts.QChart() self.polsChart.addSeries(self.polsSeries) self.polsChart.createDefaultAxes() self.polsChart.legend().hide() self.polsChart.axisX(self.polsSeries).setRange(str(0), str(self.nPolsSlider.value())) self.polsChartView = QtCharts.QChartView(self.polsChart) self.layout.addWidget(self.nPolsSlider) self.layout.addWidget(self.polsChartView) self.update_chart() def update_chart(self): self.pols = np.random.normal(0, 1, self.nPolsSlider.value()) self.polsSeries.remove(self.polsBarset) # remove old QBarSet self.polsBarset = QtCharts.QBarSet('zernike_bp') # Create new QBarSet self.polsBarset.append(list(self.pols)) self.polsSeries.append(self.polsBarset) self.polsChart.axisX(self.polsSeries).setRange(str(0), str(self.nPolsSlider.value())) app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())What happens is that the initial value of the slider (thus the initial dimension of the QBarSet) is alwais the maximum value the chart can show. It can shrink, but it cannot grow higher.
Can you guess what i'm doing wrong?
moreover i noticed a deprecation warning
DeprecationWarning: Function: 'QChart.axisX(QAbstractSeries * series) const' is marked as deprecatedWhat is the correct code?
Thanks you all for your precious support
@TommasoFurieriDO i solved it calling
self.polsChart.createDefaultAxes()inside theupdate_chart(self)function.I actually don't know if this is actually needed or if there is a better way to do so.