circle with resize handle.
Unsolved
General and Desktop
-
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.
- Click the circle. Then the resize handles appear around bounding rect.
- Move the cursor to one of handles and press and hold the mouse button.
- Drag the cursor. While draggin, the circle grows or shrink.
- 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.
- Store the bounding rectangle value in your program.
- When the circle is pressed, find out if the point is inside the bounding rectangle
- You draw the rectangle around circle in gray colour
- Now you start handling the mouse move events
- Based on the MouseMove events, you can start redrawing the circle again.