PDA

View Full Version : Why is my application's CPU usage always > 50%



richardander
2nd March 2009, 09:26
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!

wysota
2nd March 2009, 09:31
How does your paintEvent look like? Do you use timers anywhere?

richardander
3rd March 2009, 00:08
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!





> MyApp.exe!ImageView::paintEvent(QPaintEvent * __formal=0x0012c3ec) Line 961 C++
QtGuid4.dll!QWidget::event(QEvent * event=0x0012c3ec) Line 7650 C++
QtGuid4.dll!QApplicationPrivate::notify_helper(QOb ject * receiver=0x02bdcc80, QEvent * e=0x0012c3ec) Line 4051 + 0xf C++
QtGuid4.dll!QApplication::notify(QObject * receiver=0x02bdcc80, QEvent * e=0x0012c3ec) Line 4016 + 0x10 C++
QtCored4.dll!QCoreApplication::notifyInternal(QObj ect * receiver=0x02bdcc80, QEvent * event=0x0012c3ec) Line 594 + 0x13 C++
QtCored4.dll!QCoreApplication::sendSpontaneousEven t(QObject * receiver=0x02bdcc80, QEvent * event=0x0012c3ec) Line 211 + 0x38 C++
QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevic e * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5041 + 0xe C++
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++
QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevic e * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5092 C++
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++
QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevic e * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5092 C++
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++
QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevic e * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5092 C++
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++
QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevic e * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5092 C++
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++
QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevic e * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5092 C++
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++
QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevic e * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=4, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5092 C++
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++
QtGuid4.dll!QWidgetPrivate::drawWidget(QPaintDevic e * pdev=0x029db478, const QRegion & rgn={...}, const QPoint & offset={...}, int flags=5, QPainter * sharedPainter=0x00000000, QWidgetBackingStore * backingStore=0x02a568b8) Line 5092 C++
QtGuid4.dll!QWidgetBackingStore::sync() Line 1270 C++
QtGuid4.dll!QWidgetPrivate::syncBackingStore() Line 1600 C++
QtGuid4.dll!QWidget::event(QEvent * event=0x02be9fe0) Line 7790 C++
Qt3Supportd4.dll!Q3MainWindow::event(QEvent * e=0x02be9fe0) Line 1530 C++
QtGuid4.dll!QApplicationPrivate::notify_helper(QOb ject * receiver=0x0012fc94, QEvent * e=0x02be9fe0) Line 4051 + 0xf C++
QtGuid4.dll!QApplication::notify(QObject * receiver=0x0012fc94, QEvent * e=0x02be9fe0) Line 4016 + 0x10 C++
QtCored4.dll!QCoreApplication::notifyInternal(QObj ect * receiver=0x0012fc94, QEvent * event=0x02be9fe0) Line 594 + 0x13 C++
QtCored4.dll!QCoreApplication::sendEvent(QObject * receiver=0x0012fc94, QEvent * event=0x02be9fe0) Line 208 + 0x39 C++
QtCored4.dll!QCoreApplicationPrivate::sendPostedEv ents(QObject * receiver=0x00000000, int event_type=0, QThreadData * data=0x02819320) Line 1231 + 0xd C++
QtCored4.dll!QEventDispatcherWin32::processEvents( QFlags<enum QEventLoop::ProcessEventsFlag> flags={...}) Line 674 + 0x10 C++
QtGuid4.dll!QGuiEventDispatcherWin32::processEvent s(QFlags<enum QEventLoop::ProcessEventsFlag> flags={...}) Line 1177 + 0x15 C++
QtCored4.dll!QEventLoop::processEvents(QFlags<enum QEventLoop::ProcessEventsFlag> flags={...}) Line 145 C++
QtCored4.dll!QEventLoop::exec(QFlags<enum QEventLoop::ProcessEventsFlag> flags={...}) Line 195 + 0x2d C++
QtCored4.dll!QCoreApplication::exec() Line 872 + 0x15 C++
QtGuid4.dll!QApplication::exec() Line 3527 C++
MyApp.exe!main(int argc=2, char * * argv=0x02816c28) Line 182 + 0x6 C++
MyApp.exe!WinMain(HINSTANCE__ * instance=0x00400000, HINSTANCE__ * prevInstance=0x00000000, char * __formal=0x00141f07, int cmdShow=1) Line 133 + 0x12 C++
MyApp.exe!WinMainCRTStartup() Line 390 + 0x39 C
kernel32.dll!7c817067()
ntdll.dll!7c915d27()

caduel
3rd March 2009, 08:02
code would be more helpful than your stack backtrace...
do you perhaps call repaint() a lot? if so, try update() instead

wysota
3rd March 2009, 23:45
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...

barrygp
13th October 2010, 17:17
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.

squidge
13th October 2010, 19:21
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.

professionalbum
13th October 2010, 22:45
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.

barrygp
14th October 2010, 21:46
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.

void Alarm::paintEvent(QPaintEvent * event)
{

static bool inEvent = false;

if (inEvent) return;
inEvent = true;

qDebug() << "Alarm::paintEvent";
QPixmap pixmap( p_sizeHint );
QPainter painter( &pixmap );
setPixmap(pixmap);
QLabel::paintEvent(event);
inEvent = false;
}

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.

wysota
14th October 2010, 22:09
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.

barrygp
14th October 2010, 23:22
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.