在 QChartView 上实现选择

Implement selection on QChartView

我想实现基于QChart and QChartView的图表选择。 类的家族有很大的优势-easy use of openGL and animations,例如:

QLineSeries *series = new QLineSeries();
series->setUseOpenGL(true); // <==

QChart *chart = new QChart();
chart->addSeries(series);
chart->setAnimationOptions(QChart::AllAnimations); // <==

QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);    

QChartView class provides the useful zoom feature - QChartView::setRubberBand():

chartView->setRubberBand(QChartView::RectangleRubberBand);

主要问题是橡皮筋只能用于缩放,但我需要在不缩放的情况下实现水平选择,因为通常在音频编辑器中实现的功能:

现在,当我继承了 QChartView 后,我可以在选择后禁用缩放:

class ChartView : public QChartView
...
bool m_drawRubberBand;
QRubberBand m_rubberBand;
...

ChartView::ChartView(QChart *chart, QWidget *parent) 
    : QChartView(chart, parent)
{
    setRubberBand(QChartView::HorizontalRubberBand);
}
...

// Just copy-paste from the Qt 5 sources - file \Src\qtcharts\src\charts\qchartview.cpp:
/*!
If the rubber band rectangle is displayed in the press event specified by
\a event, the event data is used to update the rubber band geometry.
Otherwise, the default QGraphicsView::mouseMoveEvent() implementation is called.
*/
void ChartView::mouseMoveEvent(QMouseEvent *event)
{
    if (m_drawRubberBand && m_rubberBand.isVisible()) 
    {
        QRect rect = chart()->plotArea().toRect();
        int width = event->pos().x() - m_rubberBandOrigin.x();
        int height = event->pos().y() - m_rubberBandOrigin.y();
        if (!rubberBand().testFlag(VerticalRubberBand)) 
        {
            m_rubberBandOrigin.setY(rect.top());
            height = rect.height();
        }
        if (!rubberBand().testFlag(HorizontalRubberBand))
        {
            m_rubberBandOrigin.setX(rect.left());
            width = rect.width();
        }
        m_rubberBand.setGeometry(QRect(m_rubberBandOrigin.x(), m_rubberBandOrigin.y(), width, height).normalized());
    }
    else 
    {
        QGraphicsView::mouseMoveEvent(event);
    }
}

那我可以不在鼠标键释放事件上实现缩放动作:

void ChartView::mouseReleaseEvent(QMouseEvent *event)
{
    if (m_rubberBand.isVisible()) 
    {
        if (event->button() == Qt::LeftButton)
        {
            m_drawRubberBand = false;    
            do_nothing(); // <==
        }

    }
}

所以,我现在的问题是:

  1. 如何将可视橡皮筋的边界映射到真实图表的坐标。即,如何将选择映射到图表上的线系列?现在我收到同样错误的坐标:

      void MyView::resizeEvent(QResizeEvent *event) 
      {
          QChartView::resizeEvent(event);
    
          QRect rct(QPoint(10, 10), QPoint(20, 20));
          qDebug() << mapToScene(rct); <==
      }
    

输出:

QPolygonF(QPointF(10,10)QPointF(21,10)QPointF(21,21)QPointF(10,21))
QPolygonF(QPointF(10,10)QPointF(21,10)QPointF(21,21)QPointF(10,21))
QPolygonF(QPointF(10,10)QPointF(21,10)QPointF(21,21)QPointF(10,21))
QPolygonF(QPointF(10,10)QPointF(21,10)QPointF(21,21)QPointF(10,21))
...
  1. 如何与视图一起按比例调整现有橡胶选择的大小?

编辑: 可能是一个有用的关键字 - QGraphicsScene::setSelectionArea()

The Qt 5 chip example which provides nice rubber band selection, but the example based on QGraphicsView,不在 QChartView 上。

由于对这个答案的回复,问题已解决:

关键时刻:必须调用 QChart::mapToValue() 进行正确的坐标变换:

QPointF ChartView::point_to_chart(const QPoint &pnt)
{
    QPointF scene_point = mapToScene(pnt);
    QPointF chart_point = chart()->mapToValue(scene_point);

    return chart_point;
}

以及逆变换:

QPoint ChartView::chart_to_view_point(QPointF char_coord)
{
    QPointF scene_point = chart()->mapToPosition(char_coord);
    QPoint view_point = mapFromScene(scene_point);

    return view_point;
}

这就是我在 resizeEvent 上调整橡皮筋大小的方法。
首先,我在鼠标释放事件中保存当前橡皮筋:

void ChartView::mouseReleaseEvent(QMouseEvent *event)
{
    if (m_rubberBand.isVisible()) 
    {
        update_rubber_band(event);            
        m_drawRubberBand = false;
        save_current_rubber_band(); <==            
    }
}

其中:

void ChartView::update_rubber_band(QMouseEvent * event)
{
    QRect rect = chart()->plotArea().toRect();
    int width = event->pos().x() - m_rubberBandOrigin.x();
    int height = event->pos().y() - m_rubberBandOrigin.y();
    if (!rubberBand().testFlag(VerticalRubberBand))
    {
        m_rubberBandOrigin.setY(rect.top());
        height = rect.height();
    }
    if (!rubberBand().testFlag(HorizontalRubberBand))
    {
        m_rubberBandOrigin.setX(rect.left());
        width = rect.width();
    }
    m_rubberBand.setGeometry(QRect(m_rubberBandOrigin.x(), m_rubberBandOrigin.y(), width, height).normalized());
}

并且:

void ChartView::save_current_rubber_band()
{
    QRect rect = m_rubberBand.geometry();

    QPointF chart_top_left = point_to_chart(rect.topLeft());
    m_chartRectF.setTopLeft(chart_top_left);

    QPointF chart_bottom_right = point_to_chart(rect.bottomRight());
    m_chartRectF.setBottomRight(chart_bottom_right);
}

以及我如何在调整大小事件中重新绘制橡胶:

void ChartView::resizeEvent(QResizeEvent *event)
{
    QChartView::resizeEvent(event);

    if (m_rubberBand.isVisible())
    {
        restore_rubber_band();
    }

    apply_nice_numbers();
}

其中:

void ChartView::restore_rubber_band()
{
    QPoint view_top_left = chart_to_view_point(m_chartRectF.topLeft());
    QPoint view_bottom_right = chart_to_view_point(m_chartRectF.bottomRight());

    m_rubberBandOrigin = view_top_left;
    m_rubberBand.setGeometry(QRect(view_top_left, view_bottom_right));
}

别忘了 "nice numbers":

void ChartView::apply_nice_numbers()
{
    QList<QAbstractAxis*> axes_list = chart()->axes();
    for each(QAbstractAxis* abstract_axis in axes_list)
    {
        QValueAxis* value_axis = qobject_cast<QValueAxis*>(abstract_axis);
        if (value_axis)
        {
            value_axis->applyNiceNumbers();
        }
    }
}

这个逻辑在起作用。 调整大小前:

调整大小后: