Unsolved Need help with QGraphicsItem painter
-
Re: Draw line on mouse click
What's the problem it only draw couple of lines.#include "myitem.h" #include <QPainter> #include <QGraphicsSceneMouseEvent> MyItem::MyItem() { pressed = false; } void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { painter->drawRect(boundingRect()); painter->drawLine(mline); } QRectF MyItem::boundingRect() const { return QRectF(0,0,800,600); } void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { pressed = true; startPoint = event->scenePos().toPoint(); update(); } void MyItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { if(!pressed) { event->accept(); return; } if(pressed){ endtPoint = event->scenePos().toPoint(); mline.setPoints(startPoint,endtPoint); update(); startPoint=endtPoint; event->accept(); } QGraphicsItem::mouseMoveEvent(event); } void MyItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { pressed = false; event->accept(); update(); QGraphicsItem::mouseReleaseEvent(event); }
-
drawLine() only draws one line so what do you expect? See http://doc.qt.io/qt-5/qpainter.html#drawLine
Maybe you're looking for drawLines() ? -> http://doc.qt.io/qt-5/qpainter.html#drawLines-1 -
@Christian-Ehrlicher
I've checked scribble example and it also uses drawLine. -
@behruz-montazeri said in Need help with QGraphicsItem painter:
I've checked scribble example and it also uses drawLine.
It paints on an image without clearing it before drawing a new line. You're painting directly on a widget inside the paint event so the old content is erased.
-
@Christian-Ehrlicher
What's the workaround ?
I've selected the wrong widget ? i mean QGraphicsItem ? -
I already told you what you can use, or do it the same way it's done in the scribble example.
-
Hi
The scribble sample paints on an image so all older lines are still shown as
each new line is added to this image.In your case, you draw one line and store it in mline
If you want it to manage more than one line, you need a list
QVector<QLineF> mylines;
and each time you create a new line , you append it to mylines.
Then in paintEvent
You call
drawLines(mylines);