Qt 使用橡皮键选择在QChartView中选择QLineSeries
我有一个QChart,上面有许多QLineSeries 我已经创建了自己的图表类Qt 使用橡皮键选择在QChartView中选择QLineSeries,qt,qchart,qchartview,Qt,Qchart,Qchartview,我有一个QChart,上面有许多QLineSeries 我已经创建了自己的图表类CPlotView,该类派生自QChartView,并覆盖了MousePresseEvent、mouseMoveEvent和mouseReleaseEvent,如下所示 我想分别实现rubberband zoom和rubberband select,并且我可以根据可检查的QPushButtons(代码未显示,但它只是CPlotView的一个成员,它持有一个枚举值,该值定义了它的“模式”(缩放或选择) 但是,当橡皮键拖
CPlotView
,该类派生自QChartView,并覆盖了MousePresseEvent、mouseMoveEvent和mouseReleaseEvent,如下所示
我想分别实现rubberband zoom和rubberband select,并且我可以根据可检查的QPushButtons(代码未显示,但它只是CPlotView的一个成员,它持有一个枚举值,该值定义了它的“模式”(缩放或选择)
但是,当橡皮键拖动时,信号不会触发(也许当鼠标左键按下时,信号不会触发?):(
更新2:我已尝试在mouseReleaseEvent()调用中设置selectionArea,再次,其中一些返回为selected,远远少于调用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);
}
}
}
void ewbearinghistorychart::CEwBearingHistoryChartView::mouseReleaseEvent(QMouseEvent*事件)
{
如果(PlotRubberBand->isVisible())
{
if(PlotCursorMode==ECursorMode::eIsolate)
{
DrawRubberBand=假;
PlotRubberBand->setVisible(假);
QRect rubberband_rect=PlotRubberBand->rect();
QGraphicsView::mouseReleaseEvent(事件);
//*************新代码**************
QPolygonF p=chart()->mapToScene(PlotRubberBand->rect().normalized());
QPainterPath路径;
路径多边形(p);
chart()->scene()->setSelectionArea(路径,Qt::IntersectsItemShape);
QList selected_items=chart()->scene()->selectedItems();
qDebug()您遇到的问题是,当启用橡皮筋模式时,QChartView
重新实现MousePresseEvent
并接受事件。由于事件只向上传播QLineSeries
(QChart
和QChartView
的子对象)永远不会获得备忘录
解决方案是安装一个QEventFilter
,这样您就可以在QChartView
接受事件之前拦截该事件;但是,我还没有弄清楚如何检测单击的是否是QLineSeries
“我尝试对QGraphicsView::items返回到QLineSeries的内容进行动态_转换,但没有成功。”
下面是如何获得QLineSeries的QGraphicsItem表示形式
QChart ch;
QLineSeries srs;
QList<QGraphicsItem*> bf{ch.childItems()};
ch.addSeries(&srs);
QList<QGraphicsItem*> af{ch.childItems()};
QGraphicsItem * mf;
for(auto e: af)
if(!bf.contains(e))
mf = e;
qDebug().operator<<(mf->isUnderMouse());
QChart-ch;
QLINE系列srs;
QList bf{ch.childItems()};
ch.addSeries(和srs);
QList af{ch.childItems()};
qm*mf;
用于(自动e:af)
如果(!bf.包含(e))
mf=e;
qDebug()运算符
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);
}
}
}
QChart ch;
QLineSeries srs;
QList<QGraphicsItem*> bf{ch.childItems()};
ch.addSeries(&srs);
QList<QGraphicsItem*> af{ch.childItems()};
QGraphicsItem * mf;
for(auto e: af)
if(!bf.contains(e))
mf = e;
qDebug().operator<<(mf->isUnderMouse());