我有一个 QChart,上面有许多 QLineSeries。
我创建了自己的图表类CPlotView
,从 QChartView 派生并覆盖了 mousePressEvent、mouseMoveEvent 和 mouseReleaseEvent,如下所示。
我想分别实现橡皮筋缩放和橡皮筋选择,我可以根据可检查的 QPushButtons 选择要执行的操作(代码未显示,但它只是 CPlotView 的一个成员,它包含一个枚举值,它定义了它的“模式”(缩放或选择)
void CPlotView::mousePressEvent(QMouseEvent *event)
{
if(PlotRubberBand)
{
if (PlotCursorMode == ECursorMode::eIsolate || PlotCursorMode == ECursorMode::eZoom)
{
QRectF plotArea = chart()->plotArea();
if (PlotRubberBand->isEnabled()
&& event->button() == Qt::LeftButton
&& plotArea.contains(event->pos()))
{
DrawRubberBand = true;
PlotRubberBandOrigin = event->pos();
PlotRubberBand->setVisible(true);
PlotRubberBand->setGeometry(QRect(PlotRubberBandOrigin, QSize()));
event->accept();
}
QGraphicsView::mousePressEvent(event);
}
else
{
QChartView::mousePressEvent(event);
}
}
QChartView::mousePressEvent(event);
}
void CPlotView::mouseMoveEvent(QMouseEvent *event)
{
if (GetPlotCursorMode() == ECursorMode::eIsolate)
{
if (DrawRubberBand && PlotRubberBand->isVisible())
{
QRect rect = chart()->plotArea().toRect();
int width = event->pos().x() - PlotRubberBandOrigin.x();
int height = event->pos().y() - PlotRubberBandOrigin.y();
PlotRubberBand->setGeometry(QRect(PlotRubberBandOrigin.x(), PlotRubberBandOrigin.y(), width, height).normalized());
}
else
{
QGraphicsView::mouseMoveEvent(event);
}
}
else if (GetPlotCursorMode() == ECursorMode::eZoom)
{
if (DrawRubberBand && PlotRubberBand->isVisible())
{
QChartView::mouseMoveEvent(event);
}
else
{
QGraphicsView::mouseMoveEvent(event);
}
}
else if (GetPlotCursorMode() == ECursorMode::eSelect)
{
QGraphicsView::mouseMoveEvent(event);
}
else
{
QChartView::mouseMoveEvent(event);
}
}
void CPlotView::mouseReleaseEvent(QMouseEvent *event)
{
if (PlotRubberBand->isVisible())
{
if (PlotCursorMode == ECursorMode::eIsolate)
{
DrawRubberBand = false;
PlotRubberBand->setVisible(false);
QGraphicsView::mouseReleaseEvent(event);
QList<QGraphicsItem *> selected_items = QGraphicsView::items(PlotRubberBand->rect(), Qt::IntersectsItemShape);
qDebug() << "found " << selected_items.size() << " items"; // this appears to show a number of items, but how do I detect which items are my data series, if any are?
for (auto& item : selected_items)
{
// detect which are my data series and highlight them as selected
}
}
else
{
DrawRubberBand = false;
PlotRubberBand->setVisible(false);
QChartView::mouseReleaseEvent(event);
}
}
}
我现在可以使用缩放模式 - 单击“缩放”模式按钮,然后将绘图上的橡皮筋拖动到该区域。单击选择模式按钮会绘制橡皮筋,但不会缩放。我现在想在选择模式下选择橡皮筋中的所有数据系列。
在mouseReleaseEvent
选择模式下,我试图捕获QGraphicsItems
橡皮筋矩形内的所有选择(我希望这是返回的内容QGraphicsView::items(PlotRubberBand->rect(), Qt::IntersectsItemShape);
),但我不知道如何检测我的数据系列(我实际上混合了 QLineSeries 和QScatter 系列。
即使我没有将橡皮筋与我的任何数据相交,也会返回一些数据,因此它显然会返回场景中的其他一些QGraphicsItems
项目。QGraphicsView::items(PlotRubberBand->rect(), Qt::IntersectsItemShape);
我尝试做 adynamic_cast
由QGraphicsView::items
a返回的内容QLineSeries
,但这没有用。我忍不住想我让这比它应该的更复杂。
UPDATE1:我还尝试捕获在橡皮筋选择期间鼠标悬停在数据系列上时触发的信号:
connect(line_series, SIGNAL(hovered(const QPointF &, bool)), this, SLOT(OnDataHovered(const QPointF&, bool)));
但是当橡皮筋拖动时信号不会触发(也许当鼠标左键按下时它不会触发?)。:(
更新 2:我尝试在 mouseReleaseEvent() 调用中设置 selectionArea ,并且再次,有些返回为选中状态,远少于 call QGraphicsView::items()
,但据我所知不是我的系列:
void ewbearinghistorychart::CEwBearingHistoryChartView::mouseReleaseEvent(QMouseEvent *event)
{
if (PlotRubberBand->isVisible())
{
if (PlotCursorMode == ECursorMode::eIsolate)
{
DrawRubberBand = false;
PlotRubberBand->setVisible(false);
QRect rubberband_rect = PlotRubberBand->rect();
QGraphicsView::mouseReleaseEvent(event);
// ************** new code **************
QPolygonF p = chart()->mapToScene(PlotRubberBand->rect().normalized());
QPainterPath path;
path.addPolygon(p);
chart()->scene()->setSelectionArea(path, Qt::IntersectsItemShape);
QList<QGraphicsItem *> selected_items = chart()->scene()->selectedItems();
qDebug() << "selected items: " << selected_items.size();
// ************** /new code **************
}
else
{
DrawRubberBand = false;
PlotRubberBand->setVisible(false);
QChartView::mouseReleaseEvent(event);
}
}
}