I want to prevent zooming of a QChartView into negative x or y axis values. So I'm trying to intercept the mouseReleaseEvent in my subclass of QChartView and then modify the coordinates of the rubberBandRect before performing the zoom. But it seems the rubberBandRect coordinates are always zero after mouse release, so I think I'm not working with the correct rubberBandRect.
I'm trying to adapt based on the documentation for QChartView (emphasis added):
void QChartView::mouseReleaseEvent(QMouseEvent *event)
If the left mouse button is released and the rubber band is enabled, the event event is accepted and the view is zoomed into the rectangle specified by the rubber band.
When I tried looked at the actual values in the rubberBandRect in the code snippet below, the x/y and height/width values of the rectangle are always zero no matter how I zoomed with the cursor.
I also looked at the answer to this question: Qt How to connect the rubberBandChanged signal but in that case, they wanted the behavior in the main window, which is not what I want. Thanks!
class MyQChartView : public QtCharts::QChartView
{
Q_OBJECT
public:
MyQChartView(QWidget *parent = 0);
//...
protected:
void mouseReleaseEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
};
void MyQChartView::mouseReleaseEvent(QMouseEvent *event)
{
//always shows zeroes for the x and y position
if(event->button() == Qt::LeftButton){
std::cout << "x: " << this->rubberBandRect().x() << " y: " << this->rubberBandRect().y() << std::endl;
QChartView::mouseReleaseEvent(event);
}
//any other event
QChartView::mouseReleaseEvent(event);
}