PDA

View Full Version : temporal shapes in drawing application



parnedo
29th July 2009, 11:59
Hi.
I'm making a drawing app and i want to be able to draw lines, rectangles, ellipses ,...
I've think making like this:
1- Save the starting point

void mousePressEvent(QMouseEvent *event);
2- Draw temporal shape based on the type of cursor

void mouseMoveEvent(QMouseEvent *event);
3- Finish the draw and add it to the scene

void mouseReleaseEvent(QMouseEvent *event);


But i don't know how to make the second step, drawing the temporal shape (rectangle, circle, line, ...) while the user has the button pressed until it's released.

Some ideas?

yogeshgokul
29th July 2009, 12:11
You have to develop logic for them, there is no ready to eat pasta.;);)
Play with these 3 events and you will get what you want.
For rectangle you can use this logic.

void Widget::mousePressEvent(QMouseEvent *event)
{
origin = event->pos();
if (!rubberBand)
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
rubberBand->setGeometry(QRect(origin, QSize()));
rubberBand->show();
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
rubberBand->hide();
// determine selection, for example using QRect::intersects()
// and QRect::contains().
}

parnedo
29th July 2009, 13:55
Thank you but this is useful only for one line or one rectangle, but not for more complex shapes like polylines or ellipses.

I think that i'll create a widget like a rubberband but a little more complex, and i'll use it the same way as you with the rubberband.

Others ideas?


You have to develop logic for them, there is no ready to eat pasta.;);)
Play with these 3 events and you will get what you want.
For rectangle you can use this logic.

void Widget::mousePressEvent(QMouseEvent *event)
{
origin = event->pos();
if (!rubberBand)
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
rubberBand->setGeometry(QRect(origin, QSize()));
rubberBand->show();
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
rubberBand->hide();
// determine selection, for example using QRect::intersects()
// and QRect::contains().
}

yogeshgokul
29th July 2009, 14:07
Thank you but this is useful only for one line or one rectangle,

That I already mentioned, this is only for rectangle.



but not for more complex shapes like polylines or ellipses.

This is true.


Others ideas?
What ideas, now you just go ahead, develop logic for how to draw a eclipse and all. I think now you dont have any Qt code related question. :)