[Interest] Draw multiple rectangles in a QGraphicsItem using the mouse
David Carmona
david.carmona93 at gmail.com
Mon Oct 26 10:11:15 CET 2015
Hi Michael,
I implemented your method and it works pretty well. Thank you so much for
your help.
This is the code (ImageGraphicsSceneW subclasses ImageGraphicsScene).
void ImageGraphicsSceneW::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
origPoint=event->scenePos();
qDebug()<<"origPoint="<<origPoint;
mousePressed=true;
QGraphicsScene::mousePressEvent(event);
}
void ImageGraphicsSceneW::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if(mousePressed==true){
if(!rectangle){
rectangle=new QGraphicsRectItem;
this->addItem(rectangle);
rectangle->setPen(QPen(Qt::red,4,Qt::SolidLine));
rectangle->setPos(origPoint);
}
qDebug()<<"event.scenePos.x:"<<event->scenePos();
rectangle->setRect(0,0,(event->scenePos().x()-origPoint.x()),(event->scenePos().y()-origPoint.y()));
}
else
QGraphicsScene::mouseMoveEvent(event);
}
void ImageGraphicsSceneW::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
rectangle=NULL;
//buffer_rectangle(0,0,0,0);
mousePressed=false;
QGraphicsScene::mouseReleaseEvent(event);
}
However, I have some problems when I actually want to draw rectangles with
negatives width and height. I mean when the values of origPoint.x() and
origPoint.y() are strictly higher than scenePos().x() and scenePos().y()
respectively.
Do you have any idea of how I can deal with this problem ? Do I have to use
the QTransform class ?
Thank you so much for your help
David
2015-10-20 9:10 GMT+02:00 David Carmona <david.carmona93 at gmail.com>:
> Hi Michael,
>
> Thank you for your answer. I'm going to try it and tell you.
>
> Regards
> David
> Le 20 oct. 2015 08:42, "Michael Sué" <sue at sf.mpg.de> a écrit :
>
>> Hi,
>>
>> > I would like to create a Graphical User Interface which allows the user
>> to draw as many rectangles as he wants on an image using the left button of
>> his mouse.
>>
>> In the mousePressEvent you would create a QGraphicsRectItem, add it to
>> the graphics scene and make it "the current one".
>>
>> In mouseMoveEvent you would change the current rect's edge according to
>> the mouse position.
>>
>> The code in "paint" seems not really necessary as graphics items added to
>> the graphics scene know how to draw themselves.
>>
>> Your image you would draw in the virtual drawBackground method of your
>> graphics view.
>>
>> - Michael.
>>
>>
>>
--
David Carmona
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20151026/31e1531c/attachment.html>
More information about the Interest
mailing list