Results 1 to 4 of 4

Thread: QGraphicsView: overlay widget on top of view to speedup rubber band select/zoom?

  1. #1

    Default QGraphicsView: overlay widget on top of view to speedup rubber band select/zoom?

    Hello,

    I've got a QGraphicsView that is showing many objects, and I'm experimenting with using the rubber band to act as a zoom. However it is quite slow and laggy. I've implemented my own rubber band rectangle in QGraphicsView::drawForeground and it is also quite slow when there are lots of objects.

    Is it possible to do something like have a transparent QPainter widget ontop of the QGraphicsView widget where I can draw overlay items like a rubberband that won't slow down when there are lots of objects in the view? Or am I missing something?

    Cheers,

    Bob

  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: overlay widget on top of view to speedup rubber band select/zoom?

    I have had the same issue, and I think it is because somehow mouse movement in the rubber band widget is causing redraws in the underlying graphics view. Why, I don't know, since the rubber band widget *should* hide the view from this.

    My solution was to take a snapshot of the graphics view into a QImage and put this into a widget that exactly overlays the view below it. Rubber banding takes place in this widget, and when it ends, the overlay widget is hidden and the view below zoomed to the appropriate new rectangle.

  3. #3

    Default Re: QGraphicsView: overlay widget on top of view to speedup rubber band select/zoom?

    That sounds like an interesting solution. If you have any code snippets I'd love to see them.

    My scene consists of ~100,000 to ~500,000 items which are small, relatively simple, and all slightly different. I've done two things that have improved speed quite a bit since posting the message. The first is that I found that setting QGraphicsItem::setCacheMode(QGraphicsItem::NoCache ) on each item sped up performance quite a bit. I guess the act of caching takes longer and/or too much video memory and slows things down. The second is that following the chips example I've implemented a level-of-detail algorithm in the paint call that simplifies the rendering a bit.

    But still, drawing the rubberband is slower than I'd like, and I think drawing it in the manner you've mentioned would be best.

    Also, since my scene is essentially static elements, I'm thinking of rendering it to a cropped QPixmap and displaying that anyway. If you can post any code that would great. In particular I'm not sure how to display a widget directly over the existing view and have it line up.

    Cheers,

    Bob


    Quote Originally Posted by d_stranz View Post
    I have had the same issue, and I think it is because somehow mouse movement in the rubber band widget is causing redraws in the underlying graphics view. Why, I don't know, since the rubber band widget *should* hide the view from this.

    My solution was to take a snapshot of the graphics view into a QImage and put this into a widget that exactly overlays the view below it. Rubber banding takes place in this widget, and when it ends, the overlay widget is hidden and the view below zoomed to the appropriate new rectangle.

  4. #4
    Join Date
    Oct 2014
    Posts
    81
    Thanks
    20
    Thanked 9 Times in 9 Posts
    Qt products
    Qt5
    Platforms
    Windows
    Wiki edits
    7

    Default Re: QGraphicsView: overlay widget on top of view to speedup rubber band select/zoom?

    Isn't there a way to crop the rendering to only the affected part (commonly called "dirty" region)?
    Areas of the QGraphicsView that are outside of the rubber band widget shouldn't be repainted at all.

    In the paintEvent of your custom item you can retrieve the 'exposedRect' value from the parameter QStyleOptionGraphicsItem. It supposedly indicates the "dirty" area to be painted, but you need to set the proper item flag for this to happen:
    http://doc.qt.io/qt-5/qstyleoptiongr...xposedRect-var
    http://doc.qt.io/qt-5/qgraphicsitem.html#setFlag

    Qt Code:
    1. void MyGraphicsItem::paint( QPainter *painter,
    2. const QStyleOptionGraphicsItem *option,
    3. {
    4. // Here comes the magic:
    5. painter->setClipRect( option->exposedRect );
    6.  
    7. // All your custom painting code goes here.
    8. // ...
    9. }
    To copy to clipboard, switch view to plain text mode 
    Taken from: http://thesmithfam.org/blog/2007/02/...w-performance/ (Note it's for Qt 4.)
    Last edited by Kryzon; 11th March 2015 at 03:26.

Similar Threads

  1. rubber band zoom on a qwtpolarplot
    By john_k in forum Qwt
    Replies: 1
    Last Post: 2nd August 2014, 10:53
  2. QgraphicsView rubber band selection rectangle not visible
    By tarunrajsingh in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2013, 16:44
  3. How can I implement rubber-band with graphics view items?
    By FinderCheng in forum Qt Programming
    Replies: 6
    Last Post: 14th February 2013, 12:01
  4. Replies: 1
    Last Post: 28th February 2012, 09:54
  5. Conditional rubber band selection in QGraphicsView
    By stevel in forum Qt Programming
    Replies: 5
    Last Post: 14th January 2011, 08:32

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.