首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何防止QChartView放大为负x或y轴值

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

Stack Overflow用户
提问于 2020-09-16 18:51:27
回答 1查看 510关注 0票数 3

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

我试图根据QChartView的文档进行调整(重点是添加的):

void::mouseReleaseEvent(QMouseEvent * event )如果释放鼠标左键并启用橡皮筋,则事件事件被接受,视图被放大到由橡皮筋指定的矩形中。

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

我也看了这个问题的答案:Qt How to connect the rubberBandChanged signal,但在这种情况下,他们想要的行为在主窗口,这不是我想要的。谢谢!

代码语言:javascript
复制
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);
}
EN

回答 1

Stack Overflow用户

发布于 2020-09-23 14:32:42

经过大量的实验,我想我找到了最好的解决办法。必须做一个rubberband指针,然后findChild才能到达实际的橡皮筋。然后,鼠标释放后,我得到了图的面积坐标以及橡皮筋坐标。然后我做了一个包围盒(bounded_geometry),如果橡皮筋出了界,就限制它的值。然后我zoomIn到有界的几何图形框。仅仅修改橡皮筋是行不通的,因为这些坐标不是浮点,舍入误差会导致缩放错误。

代码语言:javascript
复制
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);
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63926286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档