QCustomPlot Discussion and Comments

rectangle that can be resized by dragging sidesReturn to overview

I'd like to implement a rectangle that can be resized by clicking and dragging its sides. I'm currently planning on using a QCPItemRect and then defining behavior that redraws the rectangle with new dimensions when the mouse is clicked on or near the rectangle sides. Is there an easier way to do this that I'm not thinking of, or a built-in resizeable item class I don't know about?

Also, this is more minor, but how can I change the cursor (say from an arrow to a hand) when it is near a rectangle edge to indicate that the rectangle can now be resized?

this is actually pretty easy to do.

i did this in 1.3 and not 2.0, but i'm sure you can adjust it to what they have.

1. subclass QCPItemRect
2. reimplement selectTest such that it checks whether you are close enough to your bounds (if it is just a rectangle, i think it should be implemented good enough. (mine has several sections so i have it check each section separately)
3. reimplement draw .again if it is just a rectangle , you dont need to do this
4. connect to mousePress,mouseMove, mouseRelease signals from QCustomPlot
5. on mouse press, check if the click is on or near your rectangle (at the edge for resize and center for move), set a marker for how far away your top left point was from the mouse press (i call it m_mousePressDelta)
6. on mouse move, and mouse release do something like this:

    QPointF mousePos=event->localPos();
    QPointF pixelPointLeft=topLeft->pixelPoint();
    QPointF pixelPointRight=bottomRight->pixelPoint();
    QPointF currentDelta=pixelPointLeft-mousePos-m_mousePressDelta;
    currentDelta.setY(0);
    switch(m_selectedPoint)
    {
        case eLeftHandle:
            topLeft->setPixelPoint(QPointF(mousePos.x(),pixelPointLeft.y()));
            if(topLeft->key()>bottomRight->key())
                topLeft->setCoords(bottomRight->key(),0);
            break;
        case eRightHandle:
            bottomRight->setPixelPoint(QPointF(mousePos.x(),pixelPointRight.y()));
            if(topLeft->key()>bottomRight->key())
                bottomRight->setCoords(topLeft->key(),1);
            break;
        case eBlock:
            topLeft->setPixelPoint(pixelPointLeft-currentDelta);
            bottomRight->setPixelPoint(pixelPointRight-currentDelta);
            break;
    }
    mParentPlot->replot(QCustomPlot::rpQueued);


changing the cursor would probably require that you set up hover event (or mouse move when hovering), check to see if you are hovering over your type of item by looping through the items and calling selectTest(this is done when you click by default, so you could probably copy the code), then you check the type by dynamic cast and if it is your type, you would call setCursor, and if it isnt, you would call setCursor(Qt::ArrowCursor), as that is the default.

thanks for the reply Ian! i'll try this soon

i guess you would want to pass the cursor's current position into the "pos" parameter of the reimplemented selectTest function?

pretty sure, yea.