Results 1 to 2 of 2

Thread: QgraphicsView rubber band selection rectangle not visible

  1. #1
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Lightbulb QgraphicsView rubber band selection rectangle not visible

    I have a QgraphicsView widget with lots of items on scene.Iam panning the view on ctr+left mouse click and zooming it to rectangle of rubber band created with left mouse button drag. Iam not able to see rubber band selection rectangle (dotted lines) whereas rubberband selection functionality works fine.Can anybody help me understand this.I use these flags in my view

    setTransformationAnchor(QGraphicsView::AnchorUnder Mouse);
    setViewportUpdateMode(QGraphicsView:: SmartViewportUpdate);
    setRenderHints(QPainter::HighQualityAntialiasing | QPainter::SmoothPixmapTransform);
    setOptimizationFlag(QGraphicsView:: DontSavePainterState,true);
    setCacheMode(QGraphicsView::CacheBackground);
    setOptimizationFlag(QGraphicsView:: DontAdjustForAntialiasing);
    setViewport(new QGLWidget);
    below are my event handlers.

    mouse press event handler

    void MyView::mousePressEvent(QMouseEvent* event)
    {
    if(event->button()==Qt::LeftButton)
    {
    if(event->modifiers()==Qt::ControlModifier)
    {
    setDragMode(QGraphicsView::NoDrag);
    m_rubberBandActive = false;
    mousepressed=true;
    m_lastDragPos = event->pos();
    return;
    }
    else
    {
    setDragMode(QGraphicsView::RubberBandDrag);
    m_rubberBandOrigin = event->pos();
    m_rubberBandActive = true;
    }
    }
    event->accept();
    }
    else
    {
    QWidget::mousePressEvent(event);
    }
    }

    mouse move event

    void MyView::mouseMoveEvent(QMouseEvent* event)
    {
    if(mousepressed)
    {
    QPointF delta = mapToScene(event->pos()) - mapToScene(m_lastDragPos);
    this->panView(delta);
    m_lastDragPos = event->pos();
    return;
    }
    event->accept();
    }



    mouse release event Handler

    void MyView::mouseReleaseEvent(QMouseEvent *event)
    {
    if (m_rubberBandActive)
    {
    QPoint rubberBandEnd = event->pos();
    QRectF zoomRectInScene = QRectF(mapToScene(m_rubberBandOrigin),mapToScene(r ubberBandEnd));

    fitInView(zoomRectInScene, Qt::KeepAspectRatio);
    m_rubberBandActive = false;
    }

    mousepressed=false;
    event->accept();
    }

    pan view

    void MyView:: panView(QPointF delta)
    {
    QPoint viewCenter(viewport()->width() / 2 + delta.x(), viewport()->height() / 2 + delta.y());
    QPointF newCenter = mapToScene(viewCenter);
    centerOn(newCenter);
    }

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QgraphicsView rubber band selection rectangle not visible

    Please use CODE tags when posting source code. Click the "Go Advanced" button when writing your message; CODE tags will be inserted when you click the "#" toolbar button. Paste your code between them.

    Did you call QGraphicsView::setInteractive() with "true" when constructing your view?

    In my code, I use an "overlay widget" for rubberband interaction. This is a QWidget exactly the same size as the plot canvas, placed exactly over the canvas rect. When the mouse is pressed, the window is shown and the rubberband is displayed there. When the mouse is released, I issue a signal from the overlay widget giving the coordinates of the selected rect, then I hide the overlay.

    I have found that when there are a very large number of items in the scene, using the drag mode of the QGraphicsView directly (as you are doing) does not give reliable performance. I think what is happening is that the scene is completely redrawn for every movement of the mouse, not only slowing everything down to almost a halt, but also making it seem like the rubberband is not being drawn. It is, but the scene isn't being redrawn fast enough. You can verify this by moving the mouse very, very slowly so the scene has enough time to be redrawn between movements. You should see the rubberband then.

    With the overlay widget, the rubber band is being drawn on the overlay widget, not on the graphics view, so there are no paint events for the graphics view until the overlay is removed. You can guarantee this if you make the overlay widget opaque, and replace its background with a pixmap of the graphics view's contents - when the mouse is pressed, you render the graphics view to a pixmap, and paint that pixmap in the overlay. Since it is opaque, none of the paint events get through to the graphics view underneath, and your rubberbanding is quick and responsive.

  3. The following user says thank you to d_stranz for this useful post:

    tarunrajsingh (4th April 2013)

Similar Threads

  1. Replies: 1
    Last Post: 28th February 2012, 09:54
  2. Non-rectangle rubber band implementation [Solved]
    By d_stranz in forum Qt Programming
    Replies: 7
    Last Post: 5th July 2011, 20:30
  3. Conditional rubber band selection in QGraphicsView
    By stevel in forum Qt Programming
    Replies: 5
    Last Post: 14th January 2011, 08:32
  4. How do I clear the rubber band after a selection is done
    By aarelovich in forum Qt Programming
    Replies: 1
    Last Post: 15th July 2010, 12:29
  5. How to draw rubber band using XOR
    By lni in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2009, 12:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.