Solved Draw line on mouse click
-
What's the problem. It doesn't respond my mouse click and release. It just draw a red point.
Is it possible to call painter outside of paint function ?void MyPoly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QPen pen; pen.setWidth(10); pen.setColor(QColor(Qt::red)); painter->setPen(pen); painter->drawLine(mLine); } void MyPoly::mousePressEvent(QGraphicsSceneMouseEvent *event) { startPoint = event->scenePos().toPoint(); QGraphicsItem::mousePressEvent(event); update(); } void MyPoly::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { endPoint = event->scenePos().toPoint(); mLine.setPoints(startPoint,endPoint); QGraphicsItem::mouseReleaseEvent(event); update(); }
-
This is because of
QGraphicsItem::mousePressEvent(event);
. It should work if you remove this line.From the doc of QGraphicsItem::mousePressEvent:
The mouse press event decides which item should become the mouse grabber (see QGraphicsScene::mouseGrabberItem()). If you do not reimplement this function, the press event will propagate to any topmost item beneath this item, and no other mouse events will be delivered to this item.
If you do reimplement this function, event will by default be accepted (see QEvent::accept()), and this item is then the mouse grabber. This allows the item to receive future move, release and doubleclick events. If you call QEvent::ignore() on event, this item will lose the mouse grab, and event will propagate to any topmost item beneath. No further mouse events will be delivered to this item unless a new mouse press event is received.```
Here is what I have used for testing ( I have also added drawing line when mouse is moving and pressed):
class MyPoly : public QGraphicsItem{ QPoint startPoint; QLine m_line; bool pressed = false; protected: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QPen pen; pen.setWidth(2); pen.setColor(QColor(Qt::darkGray)); painter->setPen(pen); painter->drawRect(boundingRect().adjusted(5, 5, -5, -5)); if(m_line.isNull()) return; pen.setWidth(10); pen.setColor(QColor(Qt::red)); painter->setPen(pen); painter->drawLine(m_line); } QRectF boundingRect() const{ return QRectF(QPointF(0,0), QPointF(200, 200)); } void mousePressEvent(QGraphicsSceneMouseEvent *event) { startPoint = event->scenePos().toPoint(); qDebug() << "Pressed, start: " << startPoint; pressed = true; update(); //QGraphicsItem::mousePressEvent(event); } void mouseMoveEvent(QGraphicsSceneMouseEvent *event){ if(pressed){ QPoint endPoint = event->scenePos().toPoint(); qDebug() << "Moving, start: " << startPoint << " end: " << endPoint; m_line.setPoints(startPoint, endPoint); update(); } QGraphicsItem::mousePressEvent(event); } void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { pressed = false; QPoint endPoint = event->scenePos().toPoint(); m_line.setPoints(startPoint, endPoint); qDebug() << "Release, start: " << startPoint << " end: " << endPoint; update(); QGraphicsItem::mouseReleaseEvent(event); } };
-
@Gojir4
Thanks. It really helps me. One of the my issues was not defining the QRectF boundingRect. -
@behruz-montazeri Glad it helps ! Can you please set the topic solved.