Results 1 to 4 of 4

Thread: Very slow repainting ofPixmaps in QGraphicsView images

  1. #1
    Join Date
    Oct 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Very slow repainting ofPixmaps in QGraphicsView images

    Using the QGraphicsView and QGraphicsScene to render pixmaps generated from a QImage. This is on QT 4.4.3.

    The pixmaps are generated and placed onto the scene from a QGraphicsItemGroup derived class, and manipulated through the returned QGraphicsPixmapItem*.

    The image is a long thin image, that is viewed by scrolling through and I draw graphics path items on.

    This works fine on Windows, but experiences major problems in Linux. Scrolling is very slow. Also drawing onto the view is also very slow. In fact, the whole application slows down hugely. This has made the application unusable.

    The application appears to be spending very little time generating the pixmaps and painting the QGraphicsItemGroup. The time spent drawing the path items is also insignificant.

    It seems that it is redrawing the image itself that is very slow. If I override the GraphicsView:aintEvent function and put a timer in there, this is very slow. (400-800ms)

    It this a known problem in Linux? It works fine in Windows. (The same paintEvent takes around 5-15ms max)

    Has anyone else experienced this? Is there a workaround for Linux?


    Code in : QGraphicsItemGroup - looks like this:

    (note: this takes max 2-3 ms)

    Qt Code:
    1. void BoreholeImage::paint(QPainter* painter,
    2. const QStyleOptionGraphicsItem* option,
    3. QWidget* widget)
    4. {
    5.  
    6. if (m_graphicsItem->type() == QGraphicsPixmapItem::Type) {
    7. // We've already loaded the image to display.
    8. QGraphicsItemGroup::paint(painter, option, widget);
    9.  
    10. // Is there anything to clean up? See below.
    11. if (m_graphicsItemToDelete) {
    12. delete m_graphicsItemToDelete;
    13. m_graphicsItemToDelete = 0;
    14. }
    15.  
    16. return;
    17. }
    18.  
    19.  
    20. m_stream->setSeekPosition(boundingRect().y());
    21. QImage image = m_stream->getImage();
    22. QPixmap pixmap = QPixmap::fromImage(image);
    23. QGraphicsPixmapItem* pixmapItem = scene()->addPixmap(pixmap);
    24. qDebug() << "BoreholeImage" << "pixmap at" << boundingRect().y();
    25. pixmapItem->setPos(0.0, boundingRect().y());
    26. pixmapItem->scale(1.0, boundingRect().height() / pixmap.height());
    27. pixmapItem->setZValue(0.0);
    28.  
    29. removeFromGroup(m_graphicsItem);
    30. scene()->removeItem(m_graphicsItem);
    31.  
    32. // Can't delete the unused rectItem here because this routine's caller
    33. // uses it again after this routine returns. Delete it on the next call.
    34. m_graphicsItemToDelete = m_graphicsItem;
    35.  
    36. m_graphicsItem = pixmapItem;
    37. addToGroup(m_graphicsItem);
    38. QGraphicsItemGroup::paint(painter, option, widget);
    39. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by drexiya; 21st October 2009 at 16:58. Reason: updated contents

  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: Very slow repainting ofPixmaps in QGraphicsView images

    QPixmap::fromImage() is a very slow method, try to avoid it. On Windows this is practically a no-op, that's why it's fast there. Cache the data instead of generating it every time you need it. And certainly avoid conversion inside paint(). Adding and removing items inside a paint() routine is a very strange design anyway.
    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
    Oct 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Very slow repainting ofPixmaps in QGraphicsView images

    Thanks Wysota,

    I agree, it is a strange design - one that I inherited.

    However, I timed this whole paint() routine as I was worried about the conversions and they are not significant compared to the delay I experience. This whole routine only takes a few ms.

    I do cache the images - but only load them as and when needed. . The routine normally returns from the first return statement. However the paintEvent in QGraphicsView takes a long time - even when the pixmaps are cached?? The paint routines for the items within the view are very quick.

    This section takes around 1ms on either platform, so I dont think the delay is here:

    Qt Code:
    1. if (m_graphicsItem->type() == QGraphicsPixmapItem::Type)
    2. {
    3. // We've already loaded the image to display.
    4. QGraphicsItemGroup::paint(painter, option, widget);
    5.  
    6. // Is there anything to clean up? See below.
    7. if (m_graphicsItemToDelete) {
    8. delete m_graphicsItemToDelete;
    9. m_graphicsItemToDelete = 0;
    10. }
    11.  
    12. return;
    13. }
    To copy to clipboard, switch view to plain text mode 

    The actual painting on the screen seems to be slow, as opposed to the conversions/calculations etc.

  4. #4
    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: Very slow repainting ofPixmaps in QGraphicsView images

    If you have lots of items, then aggregated delay will be significant. Creating and deleting items in paint() is simply broken by design. If you want to find bottlenecks in your application, run it through a profiler. Also if you add an item during paint(), the scene index will have to be recalculated and then paint() will probably be called again if your new item intersects with the group item. So instead of speedup, you get a slowdown.
    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. QGraphicsView to fast update images
    By ^NyAw^ in forum Qt Programming
    Replies: 12
    Last Post: 8th May 2009, 22:17
  2. Zooming is too slow with QGraphicsView
    By learning_qt in forum Qt Programming
    Replies: 10
    Last Post: 4th December 2008, 09:23
  3. Slow loading images
    By abbapatris in forum Qt Programming
    Replies: 10
    Last Post: 5th March 2008, 15:52
  4. QGraphicsView user-resizable images - for children
    By magland in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2007, 16:21
  5. Slow painting in QGraphicsView
    By JonathanForQT4 in forum Qt Programming
    Replies: 12
    Last Post: 16th July 2007, 09:54

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.