【问题标题】:How to prevent zooming of QChartView into negative x or y axis values如何防止将 QChartView 缩放为负 x 或 y 轴值
【发布时间】:2021-01-03 15:41:06
【问题描述】:

我想防止将 QChartView 缩放为负 x 或 y 轴值。所以我试图在我的 QChartView 子类中拦截 mouseReleaseEvent ,然后在执行缩放之前修改 RubberBandRect 的坐标。但鼠标释放后,rubberBandRect 坐标似乎总是为零,所以我认为我没有使用正确的 RubberBandRect。

我正在尝试根据 QChartView 的文档进行调整(已添加重点):

void QChartView::mouseReleaseEvent(QMouseEvent *event)

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

当我尝试在下面的代码 sn-p 中查看 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);
}

【问题讨论】:

  • rubberBand() 函数返回什么?您是否使用setRubberBand 将此值设置为不是NoRubberBand 的值?
  • @MaximSkvortsov 橡皮筋设置为矩形橡皮筋(上面的 sn-p 中没有显示)
  • 如果是,看源码:code.woboq.org/qt5/qtcharts/src/charts/… 必须设置成合适的值

标签: qt qtcharts


【解决方案1】:

经过大量实验,我想我找到了最好的解决方法。有必要制作一个 RubberBand 指针,然后 findChild 才能找到实际的橡皮筋。然后在释放鼠标后,我得到绘图区域坐标以及橡皮筋坐标。然后我制作了一个边界框(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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    相关资源
    最近更新 更多