[Interest] QGraphicsView: selection area drawing

Björn Piltz bjornpiltz at gmail.com
Thu Mar 27 10:45:47 CET 2014


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:

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>:

> 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
> http://lists.qt-project.org/mailman/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140327/27dec417/attachment.html>


More information about the Interest mailing list