如何防止QChartView缩放为负x或y轴值

问题描述

我想防止将QChartView缩放为负的x或y轴值。因此,我尝试在QChartView的子类中拦截mouseReleaseEvent,然后在执行缩放之前修改rubberBandRect的坐标。但是似乎在释放鼠标后,rubberBandRect坐标始终为零,因此我认为我无法使用正确的rubberBandRect。

我正在尝试根据QChartView的文档进行调整(强调):

void QChartView :: mouseReleaseEvent(QMouseEvent * event)

如果释放鼠标左键并启用橡皮筋,则将接受事件 event ,并且视图将放大到橡皮筋指定的矩形

当我尝试在下面的代码片段中查看rubberBandRect中的实际值时,无论我用光标如何缩放,矩形的x / y和高度/宽度值始终为零。

我还查看了以下问题的答案:Qt How to connect the rubberBandChanged signal,但在那种情况下,他们想要在主窗口中执行此操作,而这不是我想要的。谢谢!

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);
}

解决方法

经过广泛的实验,我认为我找到了最佳的解决方法。必须先创建一个橡皮筋指针,然后找到child才能到达实际的橡皮筋。然后释放鼠标后,我得到了绘图区域坐标以及橡皮筋坐标。然后,我创建了一个边界框(bounded_geometry),并在橡皮筋超出边界时限制其值。然后,我放大到有限的几何框。仅仅修改橡皮筋是行不通的,因为这些坐标不是浮点,并且舍入错误会导致缩放错误。

class MyQChartView : public QtCharts::QChartView
{
    Q_OBJECT
public:
    MyQChartView(QWidget *parent = 0):
    QChartView(parent)
    {
        myChart = new QtCharts::QChart();
        setRubberBand(QtCharts::QChartView::RectangleRubberBand);
        rubberBandPtr = this->findChild<QRubberBand *>();
        this->setChart(myChart);
        //...other things
    }
    //...
protected:
    void mouseReleaseEvent(QMouseEvent *event);
    void keyPressEvent(QKeyEvent *event);
private:
    QRubberBand * rubberBandPtr;
    QtCharts::QChart * myChart;
};


void MyQChartView::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        //first get the plot area and save the needed values (could change each time)
        QRectF plotArea = myChart->plotArea();
        qreal xbound = plotArea.x(); //values < xbound are out of bounds on x axis
        qreal ybound = plotArea.y() + plotArea.height(); // !!! note that values > ybound are out of bounds (i.e. negative y values)
        
        QPointF rb_tl = rubberBandPtr->geometry().topLeft();
        QPointF rb_br = rubberBandPtr->geometry().bottomRight();
        
        QRectF bounded_geometry = rubberBandPtr->geometry();
        
        if(rb_tl.x() < xbound){
            bounded_geometry.setX(xbound);
        }
        if(rb_br.y() > ybound){ //note that values > ybound are out of bounds
            bounded_geometry.setBottom(ybound);
        }
        
        myChart->zoomIn(bounded_geometry);        
        rubberBandPtr->close();

        return;
    }
    
    //any other event
    QChartView::mouseReleaseEvent(event);
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...