QCustomPlot Discussion and Comments

detecting MouseOver in QMLReturn to overview

I have integrated qcustomplot with qml using the information found in one of the other threads in this forum and am able to plot on it however I cant detect when my mouse moves over the plot.

In the initialization code for my plot I put the following but the code in showToolTip never runs.

void CustomPlotItem::initCustomPlot()
{
    my_CustomPlot = new QCustomPlot();
    connect(my_CustomPlot,QCustomPlot::mouseMove,this, showToolTip);
    my_CustomPlot->xAxis->setBasePen(QPen(Qt::white, 1));
.
.
.
    my_CustomPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |
                                   QCP::iSelectLegend | QCP::iSelectPlottables);
    updateCustomPlotSize();

    connect( my_CustomPlot, &QCustomPlot::afterReplot, this, &CustomPlotItem::onCustomReplot );

    my_CustomPlot->replot();
}
.
.
.
void CustomPlotItem::showToolTip(QMouseEvent *event)
{
    qDebug() << "mouse detected \n"; 
}

in qml items, you have to set 'hover' enabled i believe and then use the hover event. i've never actually tried to use tooltip, but i know when you want to do stuff with hovering, you have to enable hover and then use the hover event.

Thanks for the reply- I had

setAcceptHoverEvents(true);
in my constructor but I cant see a way to use this to get where on the x or y axis of the graph my mouse is. I could just create a mouse area over the graph in qml and then send mousex and mousey to c++ and figure out where my mouse is but I was wondering if there's an easier way to do it through qcustomplot

The issue isnt QCustomplot, if this were in a widget, then there wouldnt be an issue here. the issue is that mouse events dont get propagated to the qcustomplot when dealing with qml items.

the way we deal with this is like this:

void QCustomPlotItem::hoverMoveEvent(QHoverEvent* event)
{
    QMouseEvent mouseEvent(QEvent::MouseMove,event->posF(),Qt::NoButton,Qt::NoButton,event->modifiers());
    routeMouseEvents(&mouseEvent);
}
void QCustomPlotItem::routeMouseEvents( QMouseEvent* event )
{
    if (m_customPlot)
    {
        QMouseEvent* newEvent = new QMouseEvent( event->type(), event->localPos(), event->button(), event->buttons(), event->modifiers() );
        QCoreApplication::postEvent( m_customPlot, newEvent );
    }
}

basically every event we get in the item, we pass through using postEvent to the qcustomplot.

Thanks!