Hello! Please help me with one question. Thank you in advance.
How to select point on mouse press and then move (without mouse release)?
QCPCurve creation
QCPCurve * Route::createNewGraph( QString name, QColor color, bool prz_points, bool prz_line) { QCPCurve * cpCurve = new QCPCurve(_plot->xAxis,_plot->yAxis); QPen pen; pen.setColor(color); cpCurve->setPen(pen); cpCurve->setSelectable(QCP::stSingleData); if(!prz_line)cpCurve->setLineStyle(QCPCurve::lsNone); if(prz_line)cpCurve->setLineStyle(QCPCurve::lsLine); if(prz_points){ cpCurve->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, pen.color(), pen.color(), 10.)); QCPSelectionDecorator * decoration = new QCPSelectionDecorator(); decoration->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, Qt::blue, Qt::blue, 15.), QCPScatterStyle::spAll); cpCurve->setSelectionDecorator(decoration); cpCurve->setSelectable(QCP::stSingleData); } if(!prz_points)cpCurve->setScatterStyle(QCPScatterStyle::ssNone); cpCurve->setName(name); return cpCurve; }
slots realisation:
slot MousePress
void Route::on_mousePress(QMouseEvent* event) { if (event->buttons() == Qt::LeftButton){ setCursor(Qt::ClosedHandCursor); przPress = true; QPoint pos = event->pos(); QList<QVariant> details; layerableListAt(pos, false, &details); if(details.count() == 0) return; QVariant mMouseSignalLayerableDetails = details.first(); _plot->setInteractions(QCP::iSelectPlottables); if (details.first().value<QCPDataSelection>().isEmpty()) return; newIndex = mMouseSignalLayerableDetails.value<QCPDataSelection>().dataRange().begin(); if(oldIndex != newIndex && oldIndex != -1) { emit changeSelectionPoint(oldIndex,newIndex); oldIndex = newIndex; } } } void Route::layerableListAt(const QPointF &pos, bool onlySelectable, QList<QVariant> *selectionDetails) { QVariant details; QCPCurve * curve = (QCPCurve*)_plot->plottable(1); double dist = curve->selectTest(pos, onlySelectable, selectionDetails ? &details : 0); if (dist >= 0. && dist < 3.) { if (selectionDetails) selectionDetails->append(details); } newIndex = -1; }
slot MouseMove
void Route::on_mouseMove(QMouseEvent* event) { if ( przPress && newIndex != -1){ QPoint pos = event->pos(); QCPCurve * curve = (QCPCurve*)_plot->plottable(1); QCPDataContainer<QCPCurveData>::iterator iter; for(iter = curve->data().data()->begin();iter!=curve->data().data()->end(); iter++){ if (iter->t == newIndex){ iter->value = _plot->yAxis->pixelToCoord(pos.y()); emit pointValueChanged(newIndex, iter->value); } } _plot->replot(QCustomPlot::rpQueuedReplot); } }
MousePress
void Route::on_mouseRelease(QMouseEvent* event) { if (event->button() == Qt::LeftButton){ unsetCursor(); przPress = false; _plot->setInteractions(QCP::iRangeDrag | QCP::iSelectPlottables); if(newIndex != -1) setRescale(); } }
sreenshots:
1 - plot;
2 - when mouse pressed and released (clicked) works fine
3 - selection doesn't appear while mouse pressed and mouse moved (without mouse release)
http://dropmefiles.com/wRih6