Results 1 to 11 of 11

Thread: Why is my application's CPU usage always > 50%

  1. #1
    Join Date
    Dec 2008
    Posts
    68

    Default Why is my application's CPU usage always > 50%

    Hello,

    I wrote an application to display image.

    class ImageView : public QWidget

    I noticed that the program uses about 50% or more CPU usage all the time. By tracking the program, I noticed that the paintEvent of imageView is always called to redraw the screen. If I add Sleep( 500 ) in paintEvent(...), the CPU usage drops. But the program is slow.
    I am wondering if there is any way to enhance it?

    thank you!

  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: Why is my application's CPU usage always > 50%

    How does your paintEvent look like? Do you use timers anywhere?
    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
    Dec 2008
    Posts
    68

    Default Re: Why is my application's CPU usage always > 50%

    I don't think timer is used in my project.

    I set a breakpoint in paintEvent and here are the list in the callstack in Visual Studio. it seems the paintEvent is triggered by QT to refresh screen.

    Is there anyway to avoid the screen-refresh?

    thank you!


    Qt Code:
    1. > MyApp.exe!ImageView::paintEvent(QPaintEvent * __formal=0x0012c3ec) Line 961 C++
    2. QtGuid4.dll!QWidget::event(QEvent * event=0x0012c3ec) Line 7650 C++
    3. QtGuid4.dll!QApplicationPrivate::notify_helper(QObject * receiver=0x02bdcc80, QEvent * e=0x0012c3ec) Line 4051 + 0xf C++
    4. QtGuid4.dll!QApplication::notify(QObject * receiver=0x02bdcc80, QEvent * e=0x0012c3ec) Line 4016 + 0x10 C++
    5. QtCored4.dll!QCoreApplication::notifyInternal(QObject * receiver=0x02bdcc80, QEvent * event=0x0012c3ec) Line 594 + 0x13 C++
    6. QtCored4.dll!QCoreApplication::sendSpontaneousEvent(QObject * receiver=0x02bdcc80, QEvent * event=0x0012c3ec) Line 211 + 0x38 C++
    7. QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevice * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5041 + 0xe C++
    8. QtGuid4.dll!QWidgetPrivate::paintSiblingsRecursive(QPaintDevice * pdev=0x029db478, const QList<QObject *> & siblings={...}, int index=0, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5152 C++
    9. QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevice * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5092 C++
    10. QtGuid4.dll!QWidgetPrivate::paintSiblingsRecursive(QPaintDevice * pdev=0x029db478, const QList<QObject *> & siblings={...}, int index=3, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5152 C++
    11. QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevice * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5092 C++
    12. QtGuid4.dll!QWidgetPrivate::paintSiblingsRecursive(QPaintDevice * pdev=0x029db478, const QList<QObject *> & siblings={...}, int index=0, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5152 C++
    13. QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevice * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5092 C++
    14. QtGuid4.dll!QWidgetPrivate::paintSiblingsRecursive(QPaintDevice * pdev=0x029db478, const QList<QObject *> & siblings={...}, int index=0, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5152 C++
    15. QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevice * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5092 C++
    16. QtGuid4.dll!QWidgetPrivate::paintSiblingsRecursive(QPaintDevice * pdev=0x029db478, const QList<QObject *> & siblings={...}, int index=1, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5152 C++
    17. QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevice * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5092 C++
    18. QtGuid4.dll!QWidgetPrivate::paintSiblingsRecursive(QPaintDevice * pdev=0x029db478, const QList<QObject *> & siblings={...}, int index=0, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5152 C++
    19. QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevice * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5092 C++
    20. QtGuid4.dll!QWidgetPrivate::paintSiblingsRecursive(QPaintDevice * pdev=0x029db478, const QList<QObject *> & siblings={...}, int index=56, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5152 C++
    21. QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevice * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=5, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5092 C++
    22. QtGuid4.dll!QWidgetBackingStore::sync() Line 1270 C++
    23. QtGuid4.dll!QWidgetPrivate::syncBackingStore() Line 1600 C++
    24. QtGuid4.dll!QWidget::event(QEvent * event=0x02be9fe0) Line 7790 C++
    25. Qt3Supportd4.dll!Q3MainWindow::event(QEvent * e=0x02be9fe0) Line 1530 C++
    26. QtGuid4.dll!QApplicationPrivate::notify_helper(QObject * receiver=0x0012fc94, QEvent * e=0x02be9fe0) Line 4051 + 0xf C++
    27. QtGuid4.dll!QApplication::notify(QObject * receiver=0x0012fc94, QEvent * e=0x02be9fe0) Line 4016 + 0x10 C++
    28. QtCored4.dll!QCoreApplication::notifyInternal(QObject * receiver=0x0012fc94, QEvent * event=0x02be9fe0) Line 594 + 0x13 C++
    29. QtCored4.dll!QCoreApplication::sendEvent(QObject * receiver=0x0012fc94, QEvent * event=0x02be9fe0) Line 208 + 0x39 C++
    30. QtCored4.dll!QCoreApplicationPrivate::sendPostedEvents(QObject * receiver=0x00000000, int event_type=0, QThreadData * data=0x02819320) Line 1231 + 0xd C++
    31. QtCored4.dll!QEventDispatcherWin32::processEvents(QFlags<enum QEventLoop::ProcessEventsFlag> flags={...}) Line 674 + 0x10 C++
    32. QtGuid4.dll!QGuiEventDispatcherWin32::processEvents(QFlags<enum QEventLoop::ProcessEventsFlag> flags={...}) Line 1177 + 0x15 C++
    33. QtCored4.dll!QEventLoop::processEvents(QFlags<enum QEventLoop::ProcessEventsFlag> flags={...}) Line 145 C++
    34. QtCored4.dll!QEventLoop::exec(QFlags<enum QEventLoop::ProcessEventsFlag> flags={...}) Line 195 + 0x2d C++
    35. QtCored4.dll!QCoreApplication::exec() Line 872 + 0x15 C++
    36. QtGuid4.dll!QApplication::exec() Line 3527 C++
    37. MyApp.exe!main(int argc=2, char * * argv=0x02816c28) Line 182 + 0x6 C++
    38. MyApp.exe!WinMain(HINSTANCE__ * instance=0x00400000, HINSTANCE__ * prevInstance=0x00000000, char * __formal=0x00141f07, int cmdShow=1) Line 133 + 0x12 C++
    39. MyApp.exe!WinMainCRTStartup() Line 390 + 0x39 C
    40. kernel32.dll!7c817067()
    41. ntdll.dll!7c915d27()
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Why is my application's CPU usage always > 50%

    code would be more helpful than your stack backtrace...
    do you perhaps call repaint() a lot? if so, try update() instead

  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: Why is my application's CPU usage always > 50%

    It's not a repaint, it's triggered from the event loop. The backstore has to be getting dirty because of something. Animation or alike...
    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. #6
    Join Date
    Dec 2008
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why is my application's CPU usage always > 50%

    I am having the same issue. CPU ~50% with just one QSVGWidget in a frame with no animation. How do I check what is causing the backstore event to fire? thanks in advance.

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why is my application's CPU usage always > 50%

    If you post a compilable example showing the problem, someone could investigate and possibly tell you why it is happening.

    Alternatively, we could just guess, but that would incur a lot of trial and error on your part.

  8. #8
    Join Date
    Jun 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why is my application's CPU usage always > 50%

    I've had silly mistakes causing this issue. I've called update() in a low level function within paintEvent() which put it in an infinite loop. Did this on accident a few times. Most recently forgot to initialize a timer which caused an error. I suggest you trace your paintEvent to start through all function calls and routes.

  9. #9
    Join Date
    Dec 2008
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default

    Following is the paintEvent method that is causing high CPU usage. I cut out all the non essential code to leave the primary statements causing the high CPU utilization. I am in the process of going through each call, but let me know if there is a better way to do this, e.g. is there a painter I can reuse, or do I have to create a new one to use. With all of the code put back in, a instance of this class will display text within different colored boxes with different border shapes.
    Qt Code:
    1. void Alarm::paintEvent(QPaintEvent * event)
    2. {
    3.  
    4. static bool inEvent = false;
    5.  
    6. if (inEvent) return;
    7. inEvent = true;
    8.  
    9. qDebug() << "Alarm::paintEvent";
    10. QPixmap pixmap( p_sizeHint );
    11. QPainter painter( &pixmap );
    12. setPixmap(pixmap);
    13. QLabel::paintEvent(event);
    14. inEvent = false;
    15. }
    To copy to clipboard, switch view to plain text mode 

    thanks.

    Sorry for additional msg.. looks like the setPixmap(pixmap) is the culprit. Still, is there a better way to do this, or to stop the multiple calls to paintEvent... Still, is it ok to create an additional painter as well, or is there one existing (within widget?) that can be used, I heard that the newer version of Qt only likes one painter to be used. Thanks again.
    Last edited by wysota; 14th October 2010 at 21:07.

  10. #10
    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: Why is my application's CPU usage always > 50%

    What do you use the setPixmap call for in the paint event? You should never do that. If all you want is to change the pixmap to some other pixmap then do it when the actual change happens - use setPixmap() then and it will call update() which will call your paint event.
    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.


  11. #11
    Join Date
    Dec 2008
    Posts
    23
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why is my application's CPU usage always > 50%

    Thanks.. I see your point.. What I need is another method to do what I now have paintEvent do... then just call this new method when I need to update. Seems easy now.. didn't seem easy before :-) .. Thanks again.

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.