Results 1 to 7 of 7

Thread: Scaled QGraphicsView incredibely slow if background pixmap large (4000x4000 pixel)

  1. #1
    Join Date
    Jan 2009
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Scaled QGraphicsView incredibely slow if background pixmap large (4000x4000 pixel)

    Hello,

    following situation:
    I have a QGraphicsScene, where the background is a large image (i.e. larger than 4000x4000 pixel).
    I've tried several approaches but they are all too slow:
    1. Background Pixmap is a QGraphicsPixmapItem at the z-Level -1.
    2. QGraphicsScene::drawBackground(...) is used for drawing the background pixmap
    3. QGraphicsView::setViewport(new QGLWidget)

    Problem with 1 and 2 in scaled mode (scale=0.5): Moving the viewport with the scrollBars takes approximatly 2 seconds (I need almost smooth moving)
    Problem with 3: The moving is incredible fast, but the pixmap is completely black (too large???)

    I've tried also the following:
    QGraphicsView::drawBackground, where I am using a pre calculated transformed pixmap:
    void MyGraphicsView::drawBackground( QPainter* pPainter, const QRectF& rect )
    {
    if ( m_Scale == 1 ) {
    QGraphicsView::drawBackground( pPainter, rect );
    }
    else {
    pPainter->resetTransform();
    QRectF visibleRect( horizontalScrollBar()->value(), verticalScrollBar()->value(), viewport()->width(), viewport()->height() );
    pPainter->drawPixmap( visibleRect, m_ScaledPixmap, visibleRect );
    }
    }

    The problem with this approach is, that the painting has an automatic clipping, and this clipping is set to the scene coordinates.

    Anybody a suggestion, how I can draw a precalculated scaled pixmap in the background?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Scaled QGraphicsView incredibely slow if background pixmap large (4000x4000 pixel

    You have to remember that when you scale, the background has to be rescaled as well which takes time. When you reimplement drawBackground() I don't see you do any scaling. Do you want the background to scale or not? If not, then maybe you should do this instead:

    Qt Code:
    1. QPalette p = view.viewport()->palette();
    2. p.setBrush(QPalette::Base, QBrush(QPixmap("mypixmap")));
    3. view.viewport()->setPalette(p);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2009
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Scaled QGraphicsView incredibely slow if background pixmap large (4000x4000 pixel

    Why would I usually need to rescale the pixmap? Usually this should work fine:

    QGraphicsScene* pScene = new QGraphicsScene();
    pScene->addPixmap( "/home/user/myImage.ppm" );
    QGraphicsView* view = new QGraphicsView( pScene );
    view->scale(0.5, 0.5);

    then the view shows of course the scene scaled by 0.5. But if the pixmap is large, the scrolling through the view is far too slow.

  4. #4
    Join Date
    Jan 2009
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Scaled QGraphicsView incredibely slow if background pixmap large (4000x4000 pixel

    Ok I've solved it now
    that's the what I needed to do:

    Qt Code:
    1. void MyGraphicsView::drawBackground( QPainter* pPainter, const QRectF& rect )
    2. {
    3. if ( m_Scale == 1 ) {
    4. QGraphicsView::drawBackground( pPainter, rect );
    5. }
    6. else {
    7. pPainter->resetTransform();
    8. QRectF visibleRect( horizontalScrollBar()->value(), verticalScrollBar()->value(), viewport()->width(), viewport()->height() );
    9. pPainter->drawPixmap( QPoint(0, 0), m_ScaledPixmap, visibleRect );
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    m_ScaledPixmap is my pre-scaled pixmap. Naturally it takes some time, until the pre-scaled pixmap is computed, but therefore the scrolling works completely smooth...

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Scaled QGraphicsView incredibely slow if background pixmap large (4000x4000 pixel

    Quote Originally Posted by Jocker16 View Post
    But if the pixmap is large, the scrolling through the view is far too slow.
    Because the pixmap has to be scaled each time you repaint it At least enable caching of the item (a better approach would be to cut the pixmap into several smaller ones and then enable caching). But I don't see what it has to do with the background...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    Jocker16 (13th January 2010)

  7. #6
    Join Date
    Jan 2009
    Posts
    6
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Scaled QGraphicsView incredibely slow if background pixmap large (4000x4000 pixel

    And which caching should I use? ItemCoordinateCache or DeviceCoordinateCache?
    I'm not sure which one is better here.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Scaled QGraphicsView incredibely slow if background pixmap large (4000x4000 pixel

    It depends whether you will want to scale the item or not. DeviceCoordinateCache will recompute the cache each time the effective scale of the item changes but it will retain full quality of the item.. ItemCoordinateCache will not recompute the cache but quality of the item will degrade.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 6
    Last Post: 2nd March 2018, 04:39
  2. Replies: 0
    Last Post: 6th April 2009, 01:20
  3. Inaccurate MouseEvent position over a Scaled Pixmap
    By ramstormrage in forum Newbie
    Replies: 9
    Last Post: 26th May 2008, 10:48
  4. psql large object and pixmap
    By mkarakaplan in forum Newbie
    Replies: 2
    Last Post: 25th November 2007, 11:46
  5. How to get background pixmap?
    By philipp1 in forum Qt Programming
    Replies: 2
    Last Post: 4th November 2006, 08:44

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.