[Interest] QGraphicsView: selection area drawing

Christian Gagneraud chgans at gna.org
Fri Mar 28 01:08:51 CET 2014


On 27/03/14 22:45, Björn Piltz wrote:
> It is not so clean to let the zoom selection influence the
> scene.(Imagine you have two views of the same scene). Here is how we do it:

Hi Björn,

I knew I didn't do it the right way (tm), but I couldn’t come with 
something else.
Your solution works like a charm, thanks a lot for sharing!

Chris

> void ZoomView::mousePressEvent(QMouseEvent* e)
> {
>      QGraphicsView::mousePressEvent(e);
>
>      p->startDrag = e->pos();
>      if (!p->rubberBand)
>          p->rubberBand = new QRubberBand(QRubberBand::Rectangle,
> viewport());
>      p->rubberBand->setGeometry(QRect(p->startDrag, QSize()));
>      p->rubberBand->show();
> }
>
> void ZoomView::mouseMoveEvent(QMouseEvent* e)
> {
>      QGraphicsView::mouseMoveEvent(e);
>
>      if (p->rubberBand)
>          p->rubberBand->setGeometry(QRect(p->startDrag,
> e->pos()).normalized());
> }
>
> void ZoomView::mouseReleaseEvent(QMouseEvent* e)
> {
>      QGraphicsView::mouseReleaseEvent(e);
>
>      p->rubberBand->hide();
>      QRect rect = p->rubberBand->geometry().normalized();
>      if (rect.width() > 5 && rect.height() > 5)
>          fitInView(QRectF(mapToScene(rect.topLeft()),
> mapToScene(rect.bottomRight())), Qt::KeepAspectRatio);
>      delete p->rubberBand;
>      p->rubberBand=  0;
> }
>
>
>
> 2014-03-26 23:44 GMT+01:00 Christian Gagneraud <chgans at gna.org
> <mailto:chgans at gna.org>>:
>
>     Hi there,
>
>     I've implementing a "zoom area" tool on a QGraphicsView, while dragging
>     the mouse it draws the zoom area in a (visually) similar way as the
>     RubberBandDrag selection area. I've looked into the source code of
>     QGraphicsView but couldn't find which color is used to draw it.
>     I've pinned down the QStyleOptionRubberBand and QStyleOption::OptionType
>     but I don't get how I should use it.
>     My naive implementation draws the area using a QGraphicsRectItem, but
>     I've discovered that QGraphicsView draws the selection area using a
>     painter in paintEvent() (after calling drawForeground()).
>     What is the best way to draw the zoom area, should I handle this in
>     drawForeground()? The code in QGraphicsView looks over-complicated to me
>     compared with using a QGraphicsRectItem.
>
>     Any thought or advice?
>
>     Chris
>
>
>     _______________________________________________
>     Interest mailing list
>     Interest at qt-project.org <mailto:Interest at qt-project.org>
>     http://lists.qt-project.org/mailman/listinfo/interest
>
>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>




More information about the Interest mailing list