QChartView,RubberBand和鼠标右键行为

时间:2016-11-24 09:55:43

标签: c++ qt qtchart

我上课,派生自QChartView,我已启用橡皮筋选择

MyChartView::MyChartView(QChart* chart)
:QChartView(chart)
{
    setMouseTracking(true);
    setInteractive(true);
    setRubberBand(RectangleRubberBand);
}

Qt文档说明

  

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

我不想让右键缩小。我试图覆盖mouseReleaseEvent

void MyChartView::mouseReleaseEvent(QMouseEvent *e)
{
    if(e->buttons() == Qt::RightButton)
    {
        std::cout << "my overriden event" << std::endl;
        return; //event doesn't go further
    }
    QChartView::mouseReleaseEvent(e);//any other event
}

但它不会打印任何内容。

如何更改此行为?

1 个答案:

答案 0 :(得分:2)

问题解决方案非常简单。我刚刚混合了button()buttons()个函数。以下代码正常运行:

void MyChartView::mouseReleaseEvent(QMouseEvent *e)
{
    if(e->button() == Qt::RightButton)
    {
        std::cout << "my overriden event" << std::endl;
        return; //event doesn't go further
    }
    QChartView::mouseReleaseEvent(e);//any other event
}