circle with resize handle.



  • Hi,

    I draw a circle on the QGraphicsScene using QGraphicsPixmapItem.

    #include <QtWidgets>
    class MyGraphicsCircle : public QGraphicsEllipseItem
    {
    public:
        MyGraphicsCircle(QGraphicsItem *parent = 0) : QGraphicsEllipseItem(parent)
        {
            setRect(50, 50, 100, 100);
        }
    protected:
        void mousePressEvent(QGraphicsSceneMouseEvent *event)
        {
            startingPos = event->pos();
            grabMouse();
        }
        void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
        {
            QRectF currentRect = rect();
            currentRect.setBottomRight(currentRect.bottomRight() + (event->pos() - startingPos));
            setRect(currentRect);
            startingPos = event->pos();
            QGraphicsEllipseItem::mouseMoveEvent(event);
        }
        void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
        {
            ungrabMouse();
        }
    private:
        QPointF startingPos;
    };
    
    int main(int argc, char **argv)
    {
        QApplication a(argc, argv);
        QGraphicsView gv;
        QGraphicsScene *gs = new QGraphicsScene(0, 0, 500, 500);
        gv.setScene(gs);
        gs->addItem(new MyGraphicsCircle);
        gv.show();
        return a.exec();
    }
    

    What I would like to do is the following.

    1. Click the circle. Then the resize handles appear around bounding rect.
    2. Move the cursor to one of handles and press and hold the mouse button.
    3. Drag the cursor. While draggin, the circle grows or shrink.
    4. Release the mouse button. The circle with desired size is drawn.

    Any idea how to achieve this goal?



  • In order to achieve this, there is not direct way of doing this. Here is how you can achieve this.

    1. Store the bounding rectangle value in your program.
    2. When the circle is pressed, find out if the point is inside the bounding rectangle
    3. You draw the rectangle around circle in gray colour
    4. Now you start handling the mouse move events
    5. Based on the MouseMove events, you can start redrawing the circle again.

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.