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

How to maintain QChart resetZoom level after QValueAxis::setRange and/or QBarCategoryAxis::setCategories



  • Hello, I'm trying to make a QCandlestickSeries that updates a QChart in real-time. I've gotten to the point where everything works, but one feature. Whenever I add the new data, which updates values correctly, I append a QString representing a QDateTime for the QBarCategoryAxis::setCategories call to update my x-Axis of dates. I also update the range of the y-Axis which is a QValueAxis by calling QValueAxis::setRange. If I call either of these functions, I looked in to the source code of the QChartPrivate stuff and it appears that they both make calls to QChart::zoomReset(). There is no ability to get the current zoom factor's value, nor does Qt seem to even hold on to it in its Private implementation of QChart . I'd really like to have the effect where I can save the current settings for my QChartPrivate's m_presenter member variable and restore the state such that when I add new data and set new ranges, I can put the presenter back to its original zoom and scroll location. Is there anyway for me to do this with the public interface without editing Qt source code? I essentially just need a boolean that flips whether I want to preserve my zoom and center upon new ranges, but there doesn't seem a possible way to do this without "hacking" the chart to zoom back to its previous value, which I'm having a hard time doing with a QBarCategoryAxis since the min and max aren't pixel values on the chart, but rather just of typeQString.



  • I made a mistake in understanding the what QBarCategoryAxis::setCategories did. So to fix my issue, I only call QBarCategoryAxis::setCategories on startup when I seed some historical data and after that I only call QBarCategoryAxis::append for real-time updates fed to the program. The call to QBarCategoryAxis::append does not set the range without the user's permission, so this is the behavior I wanted. I also only need to call QValueAxis::setRange on startup, otherwise I don't want to call it again. Now it exhibits the desired behavior.


Log in to reply