I have one problem, i cant hide label and tracer after moving mouse cursor from one chart to another. My task is to make a tracer with a label appear when you move the mouse cursor over the chart, and when you move the cursor to another chart, it should disappear on the chart on which it was.
The problem is that the tracer does not disappear after switching to another chart.
void Plotter::onMouseMoveEvent(QMouseEvent* event)
{
QPoint mousePos = event->pos();
double coordX = xAxis->pixelToCoord(event->pos().x());
double coordY = yAxis->pixelToCoord(event->pos().y());
if (rect().contains(mousePos))
{
customizeTracer(coordX, coordY);
QPointF tracerCoords = mTracer->position->coords();
double xTracerCoord = tracerCoords.x();
double yTracerCoord = tracerCoords.y();
QTime time = QTime(0, 0).addMSecs(static_cast<int>(xTracerCoord));
QString formattedTime = time.toString("HH:mm:ss.zzz");
customizeLabel(xTracerCoord, yTracerCoord, formattedTime);
replot();
}
else
{
removeItem(mTracer);
removeItem(mLabel);
replot();
}
}
void Plotter::customizeTracer(const double coordX, const double coordY)
{
mTracer->setGraph(graph());
mTracer->setStyle(QCPItemTracer::tsCircle);
mTracer->position->setType(QCPItemPosition::ptPlotCoords);
mTracer->position->setCoords(coordX, coordY);
mTracer->setGraphKey(coordX);
mTracer->setPen(QPen(QColor("red")));
mTracer->setSize(5);
mTracer->setBrush(QColor("red"));
mTracer->setInterpolating(true);
mTracer->setVisible(true);
mTracer->updatePosition();
}
void Plotter::customizeLabel(const double coordX, const double coordY, const QString& formattedTime)
{
mLabel->setPositionAlignment(Qt::AlignLeading | Qt::AlignBottom);
mLabel->position->setCoords(coordX, coordY);
mLabel->setText(QString("Time %1 | Position %2").arg(formattedTime).arg(coordY));
}
OK, i just add some functions.
void Plotter::onMouseMoveEvent(QMouseEvent* event)
{
QPointF mousePos = event->pos();
QCPAbstractPlottable* plottable = plottableAt(mousePos);
if (plottable != nullptr)
{
double coordX = xAxis->pixelToCoord(event->pos().x());
double coordY = yAxis->pixelToCoord(event->pos().y());
customizeTracer(coordX, coordY);
QPointF tracerCoords = mTracer->position->coords();
double xTracerCoord = tracerCoords.x();
double yTracerCoord = tracerCoords.y();
QTime time = QTime(0, 0).addMSecs(static_cast<int>(xTracerCoord));
QString formattedTime = time.toString("HH:mm:ss.zzz");
customizeLabel(xTracerCoord, yTracerCoord, formattedTime);
mTracer->setVisible(true);
mLabel->setVisible(true);
replot();
}
else
{
mTracer->setVisible(false);
mLabel->setVisible(false);
replot();
}
}