Results 1 to 11 of 11

Thread: Optimize QGraphicsItem update

  1. #1
    Join Date
    Oct 2009
    Posts
    70

    Default Optimize QGraphicsItem update

    Hi, I want to show a video with QGraphicsView and QGraphicsItem.

    I need to show every 40 ms a QImage. I use a QTimeLine and a QGraphicsItem.

    The problem is that when I call the update() on the QTimeLine slot, there is a lot of time ( about 10 ms ) between the update() call and the paint event of the item.

    I know that there isn't an equivalent repaint() for the QGraphicsItem, but anyone know a way to reduce this time ??

    Thanks

  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: Optimize QGraphicsItem update

    The time depends on what your application is doing apart showing this one item. Without knowing any details it is not possible to suggest an optimization.
    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
    70

    Default Re: Optimize QGraphicsItem update

    The application is doing anything. I try to use QApplication:rocessEvents() after the update() call but it doesn't change nothing.

    Any ideas?
    Last edited by paolom; 21st March 2011 at 11:04.

  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: Optimize QGraphicsItem update

    Please show us some code (especially the part where you set the frames on the item). ProcessEvents will not help because the application will be processing events anyway. Also please state where you get the frames from.
    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.


  5. #5
    Join Date
    Oct 2009
    Posts
    70

    Default Re: Optimize QGraphicsItem update

    This is the code:

    Qt Code:
    1. /* The QTimeLine slot called every 40 ms */
    2. void VideoItem::goToFrame(int frameNumber)
    3. {
    4. m_currentFrameNumber = frameNumber;
    5. updateFrame();
    6. }
    7.  
    8. void VideoItem::updateFrame()
    9. {
    10. if ( m_file )
    11. {
    12. QImage im;
    13. bool finished;
    14. if ( ( m_currentFrameNumber - m_previousFrameNumber ) == 1 )
    15. {
    16. im = m_file->getNextFrame(finished);
    17. }
    18. else
    19. {
    20. for ( int i = 0 ; i < ( m_currentFrameNumber - ( m_previousFrameNumber + 1 ) ) ; i++ )
    21. m_file->getNextFrame(finished,false);
    22.  
    23. if ( !finished )
    24. im = m_file->getNextFrame(finished);
    25.  
    26. }
    27.  
    28. m_previousFrameNumber = m_currentFrameNumber;
    29.  
    30. if ( !im.isNull() && !finished)
    31. {
    32. m_counter++;
    33. m_frameCount = m_currentFrameNumber;
    34.  
    35. setCurrentImage(im);
    36. }
    37. else
    38. {
    39.  
    40. stop();
    41.  
    42. emit videoFinished();
    43. }
    44. }
    45. }
    46.  
    47. void VideoItem::setCurrentImage(QImage im)
    48. {
    49. m_currentImage = im;
    50. update();
    51. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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: Optimize QGraphicsItem update

    What is m_file and how does its getNextFrame work? Does it load an image from a file? Also, how large is each frame?
    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.


  7. #7
    Join Date
    Oct 2009
    Posts
    70

    Default Re: Optimize QGraphicsItem update

    getNextFrame() gets a frame from an mpeg-2 video using the ffmpeg. I've tested() the time for this method and it spends few ms. The bottleneck is between the update() call from setCurrentImage() and the really paint time. For this time tests I've use QElapsedTimer.

    getNextFrame() gets a frame from an mpeg-2 video using the ffmpeg. I've tested() the time for this method and it spends few ms. The bottleneck is between the update() call from setCurrentImage() and the really paint time. For this time tests I've use QElapsedTimer.


    Added after 4 minutes:


    This is the painter code:

    Qt Code:
    1. void VideoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. painter->setRenderHint(QPainter::SmoothPixmapTransform,false);
    4. painter->setRenderHint(QPainter::HighQualityAntialiasing,false);
    5. painter->setRenderHint(QPainter::NonCosmeticDefaultPen,false);
    6. painter->drawImage(this->boundingRect(),currentImage());
    7. }
    8.  
    9. QRectF VideoItem::boundingRect() const
    10. {
    11. return QRectF(0,0,720,576);
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by paolom; 21st March 2011 at 11:14.

  8. #8
    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: Optimize QGraphicsItem update

    The delay you experience is probably caused by some other events being processed first and additionally by the need to setup the item transformation by graphics view architecture. There are some optimizations you can do but you need to find the true bottleneck. QElapsedTimer will not give you reliable result because it is influenced by the environment. It measures a time span and not the time used by the process. If scheduler doesn't schedule your process for some time, this time will be included in QElapsedTimer measurements. Show me the paint() routine of your item, tell me the number of elements the scene contains and any custom flags you have set on any of the items or custom settings of the scene. And the platform you are running on.
    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.


  9. #9
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Optimize QGraphicsItem update

    I'm not sure for 100%, but I think paint event is synchronized with screen refresh (in LCD displays it is usually 60Hz).
    At least during of some testing I note that paint was call not more often then refresh rate of the monitor.
    So this may be source of delay between update call and paint event.
    I would check how Qt code solves this kind of problems (QMove, QLabel, QSvgRenderer).

  10. #10
    Join Date
    Oct 2009
    Posts
    70

    Default Re: Optimize QGraphicsItem update

    Running on Mac Os X 10.6.6.

    The scene contains one item which is the base item (QGraphicsRectItem ) with any custom flags ( it has the default flags).

    This base item has a child item (called GridItem which is a QGraphicsRectItem ) with this flags ( other than the default flags ):

    Qt Code:
    1. setFlag(QGraphicsItem::ItemClipsChildrenToShape,true);
    2. setFlag(QGraphicsItem::ItemIsFocusable,true);
    To copy to clipboard, switch view to plain text mode 

    The gridItem has a child item ( called ImageItem which is QGraphicsObject ) with this flags ( other than the default flags ) :

    Qt Code:
    1. setFlag(QGraphicsItem::ItemIsSelectable,false);
    2. setFlag(QGraphicsItem::ItemIsFocusable,true);
    To copy to clipboard, switch view to plain text mode 

    The ImageItem has a ChildItem which is the VideoItem ( QGraphicsObject) which has any flags other than the default flags.

    The scene has not any custom settings.

    Qt Code:
    1. void VideoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. painter->setRenderHint(QPainter::SmoothPixmapTransform,false);
    4. painter->setRenderHint(QPainter::HighQualityAntialiasing,false);
    5. painter->setRenderHint(QPainter::NonCosmeticDefaultPen,false);
    6. painter->drawImage(this->boundingRect(),currentImage());
    7. }
    8.  
    9.  
    10. /* QGraphicsView option */
    11. View::View(QGraphicsScene *scene, MPWindow * win, MPGridBoxItem * box, QWidget *parent)
    12. :QGraphicsView(scene,parent),
    13. _window(win),
    14. _box(box)
    15. {
    16.  
    17. setRenderHint(QPainter::SmoothPixmapTransform,false);
    18. setRenderHint(QPainter::HighQualityAntialiasing,false);
    19. setRenderHint(QPainter::NonCosmeticDefaultPen,true);
    20.  
    21. QRectF rect = scene->sceneRect();
    22. setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    23. resize(rect.width() ,rect.height());
    24.  
    25. setAlignment(Qt::AlignLeft);
    26. QBrush brush(QColor(Qt::black));
    27. brush.setStyle(Qt::SolidPattern);
    28. setBackgroundBrush(brush);
    29. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    30. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    31. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    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: Optimize QGraphicsItem update

    Why do you need children clipping? It slows down things quite significantly. Furthermore activate OpenGL viewport for your view, this should make rendering faster. I would also advise to use QtConcurrent-based thread to prefetch frames from the video, especially if you're targetting multi-core machines.
    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. Update scene after moving QGraphicsItem
    By rogerholmes in forum Newbie
    Replies: 1
    Last Post: 19th January 2010, 05:08
  2. How to optimize qt libs for embedded x86?
    By yangyunzhao in forum Installation and Deployment
    Replies: 2
    Last Post: 30th June 2009, 09:47
  3. QGraphicsItem does not update
    By zgulser in forum Qt Programming
    Replies: 1
    Last Post: 5th February 2009, 14:44
  4. Optimize Qt Library
    By Svaths in forum Qt for Embedded and Mobile
    Replies: 11
    Last Post: 15th July 2007, 22:25
  5. Replies: 2
    Last Post: 26th April 2006, 10:43

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.