Solved How to Create QGraphicsItem(Lines, Rectangles, etc.) in QGraphicsScence by mouse?
-
@JonB And I also wondering I should override mousePressedEvent in QGraphicsScence or QGraphicsView, both of them have mousePressedEvent but I don't know which to override.
-
@darrenleeleelee1
If you want to create those "freehand" letter drawings as aGraphicsItem
you would have to add it as aQGraphicsPathItem
composed from a multi-itemQPainterPath
. Unless you are prepared to do it as aQGraphicsPixmapItem
, and lose the ability to manipulate the points.It probably doesn't matter which of
QGraphicsScence
orQGraphicsView
to catch the mouse event, so long as it works. I did refer you to the DiagramScene example which seems to use the scene's mouse event. -
@JonB Sorry, maybe I was misled you, I want a pen that I can draw anything I want, kind of doodle?Just one stroke when the mouse released the stroke become an object.
kind of this. -
@darrenleeleelee1
You might get some clues from https://doc.qt.io/qt-5/qtwidgets-widgets-scribble-example.html in combination with https://forum.qt.io/topic/94586/draw-line-on-mouse-click & https://forum.qt.io/topic/94622/need-help-with-qgraphicsitem-painter . -
@JonB ok, sorry I still have some question.
How can I do like I pressed mouse and hold, to draw a line until I released mouse, the line will become an object. -
@darrenleeleelee1
I don't know what to say, it's just code you need to write. Note where the mouse goes down, note where the mouse is released and at that stage create aQGraphicsLineItem
or whatever between those two points. -
@JonB But if I create the QGraphicsLineItem at mousedReleasd, when I holding the mouse pressed, I can't see the line.
-
@darrenleeleelee1 said in How to Create QGraphicsItem(Lines, Rectangles, etc.) in QGraphicsScence by mouse?:
But if I create the QGraphicsLineItem at mousedReleasd, when I holding the mouse pressed
How can you get mouse released event if you hold mouse button pressed? Can you please explain better?
-
@jsulm I want to override mouseMoveEvent to get my mouse position when my mouse is holding.
-
@jsulm I am thinking if I can change the existing QGraphicsLineItem to keep update to where my mouse position, and the start position is the moused click position.But I don't know how to adjust existing QGraphicsLineItem.
-
@darrenleeleelee1 said in How to Create QGraphicsItem(Lines, Rectangles, etc.) in QGraphicsScence by mouse?:
But I don't know how to adjust existing QGraphicsLineItem.
Again, I don't know what to say. You have
QGraphicsLineItem::setLine()
. I don't see the problems you see. -
@JonB You are right, sorry I am thinking too complicated