QCustomPlot Discussion and Comments

Using + and - to zoomReturn to overview

I am generating mouse wheel events but they are not having any zoom effect. What am I missing here? Here are some snippets of code:

this->installEventFilter(this);
this->setFocusPolicy(Qt::StrongFocus);
radarPlot->setFocus();
connect(radarPlot, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel(QWheelEvent*)));


void radarWidget::mouseWheel(QWheelEvent *event) // SLOT from the mouse wheel
{
    qDebug() << __FUNCTION__ << event << ":" << event->pos() << ":" << event->pos().x() << ":" << event->pos().y() << "delta:" << event->delta();
    if (event->pos().y() > radarPlot->axisRect()->bottom())                  // Zoom only X
        radarPlot->axisRect()->setRangeZoomAxes(radarPlot->xAxis, NULL);
    else if (event->pos().x() < radarPlot->axisRect()->left())               // Zoom only Y
        radarPlot->axisRect()->setRangeZoomAxes(NULL, radarPlot->yAxis);
    else                                                                     // Zoom both
        radarPlot->axisRect()->setRangeZoomAxes(radarPlot->xAxis, radarPlot->yAxis);
}

bool radarWidget::eventFilter(QObject * /* o */, QEvent *e)
{
    if ( e->type() == QEvent::KeyPress ) {
        QKeyEvent *k = (QKeyEvent *) e;

        QString keyStr = QKeySequence(k->key()).toString();
        if(keyStr == "+" || keyStr == "-" || keyStr == "=" || keyStr == "_") {

            // Get the mouse location
            QPoint p = this->mapFromGlobal(QCursor::pos());
            p -= this->childrenRect().topLeft(); // Account for the margin

            // Create a fake mouse wheel event
            QWheelEvent *evt;
            if(keyStr == "+")
                evt = new QWheelEvent(p,  1200, 0, k->modifiers());
            else if(keyStr == "=")
                evt = new QWheelEvent(p,  120, 0, k->modifiers());
            else if(keyStr == "-")
                evt = new QWheelEvent(p, -120, 0, k->modifiers());
            else if(keyStr == "_")
                evt = new QWheelEvent(p, -1200, 0, k->modifiers());
            else return false;

            emit mouseWheel(evt);

            delete evt;
            return true; // consume event
        }
    }
    return false;
}

As you can see, I use qDebug() in the mouse event handler so I can see that mouse wheel events are generated either by scrolling the mouse, or using the + and - keys. The thing is, when I use the +- keys, no zooming actually takes place. Here's the output from that qDebug() call:

mouseWheel QWheelEvent(QPoint(0,0) QPoint(0,120) )  : QPoint(639,255) : 639 : 255 delta: 120
mouseWheel QWheelEvent(QPoint(0,0) QPoint(0,120) )  : QPoint(639,255) : 639 : 255 delta: 120
mouseWheel QWheelEvent(QPoint(0,0) QPoint(0,120) )  : QPoint(639,255) : 639 : 255 delta: 120
mouseWheel QWheelEvent(QPoint(0,0) QPoint(0,0) )  : QPoint(650,284) : 650 : 284 delta: 120
mouseWheel QWheelEvent(QPoint(0,0) QPoint(0,0) )  : QPoint(650,284) : 650 : 284 delta: 120
mouseWheel QWheelEvent(QPoint(0,0) QPoint(0,0) )  : QPoint(650,284) : 650 : 284 delta: 120

The first three are real mouse scroll events. The last three are faked ones.

I'm using qcustomplot-1.2.1
Thanks!

I think the only think that miss, is this call:

mMainPlot->setInteractions(QCP::iRangeZoom)

**(mMainPlot = QCustomPlot)
Go see if you need some other interactions with the plot and "add" the ones you need !

You might need to set the range zoom (the orientation) on each QCPAxisRect with :
r->setRangeZoom(Qt::Horizontal|Qt::Vertical);

but I think it is already set like this by default.

Thanks Oliver - I actually already have that, I just forgot to include it in the snippet above :-(

// Enable zoom & pan
radarPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);

A couple of things i see:
1. setRangeZoomAxis doesnt zoom. it just sets which axes are allowed to zoom.
2. you dont emit events, you use QApplication::postEvent.
3. why dont you just call scaleAxis directly from your event filter instead of doing this roundabout method through the scroll wheel?

Ian is right!

You don't emit the signal event!

you should use something like this at the end of your function of mouseWHeelEvent:

QCustomPlot::wheelEvent(event);

Thanks Ian & Oliver. Forgive my ignorance but I can't find anything called scaleAxis - I seem to be missing something really obvious. Could you perhaps be a bit more explicit? Sorry to waste your time.

By the way, I have tried each of the following (by un-commenting), but none of them seem to have any effect..

        //  emit mouseWheelEvent(evt); // renamed, previously called mouseWheel()
        //  radarPlot->mouseWheel(evt);
        //  wheelEvent(evt);
        //  QApplication::postEvent(this, evt);

I agree that simply re-scaling the plot directly would be much less of a roundabout way of doing it.

Ian, I think you mean scaleRange() not scaleAxis()?

yes, my bad, i meant scaleRange.