3

I have a problem with Paintevent in Qt. Paintevent thread is utilizing almost all amount of CPU when started.

Code in constructor of my main widget.

Display_dialog::Display_dialog(QWidget *parent):QDialog(parent),
ui(new Ui::Display_dialog)
{
     ui->setupUi(this);
     pRedraw_Timer = new QTimer(this);
     connect( pRedraw_Timer, SIGNAL(timeout()), this, SLOT(update()));
     pRedraw_Timer->start( 15 );
}

And in my paintevent,

 void MyDialog::paintEvent(QPaintEvent *event)
 {
 }

Nothing at all in Paintevent!!!But running this piece of code consumes 100% of CPU [CPU is Single core 1 Ghz Processor].

And when I stop(pRedraw_Timer->stop()) this pRedraw_Timer which calls update, for ex: On a button click the usage comes down to 1% or under 10% almost instantly!!!!

My requirement is plotting a graph depending upon input from serial port. So i need to constantly update the view using update. But while plotting, any interrupt(high priority) seems to disturb the plotting, as cpu switches for handling the interrupt. After handling interrupt, plotting comes back to normal. My question is how and why is this paintevent thread consuming 100% CPU even if its not doing anything at all. How can I change this Scenario?

EDIT(for kuba ober): Well there are several other functions and slots. But nothing is invoked unless I call it or Trigger a Signal. I am not doing anything at all. Just after running the program, it consumes all cpu. Remember only constructor of main dialog is called.

ScarCode
  • 3,074
  • 3
  • 19
  • 32
  • Try to redraw only when receiving new data, redrawing every 15 ms can be really heavy. – scai Jun 26 '12 at 13:19
  • Yeah..I tried redrawing at every 150 ms...But still consuming all CPU....And also the drawing looks clumsy...Can't delay it too much as I am plotting real-time data – ScarCode Jun 26 '12 at 13:24
  • Use `callgrind` to see where it's spending all it's time. – cmannett85 Jun 26 '12 at 16:18
  • already isolated issue is in paintevent.. – ScarCode Jun 26 '12 at 17:31
  • Please post a minimum test case. I can't reproduce this problem with an empty QDialog either on Windows or on OS X. Besides, it's *not* the `paintEvent()` that consumes that much CPU, because calling an empty function every 15ms won't do it. It's some code internal to Qt - probably paint code of other widgets -- that is doing that. Are there any children in that dialog? Show your code, I can then profile it and see where it really is spending the time. – Kuba hasn't forgotten Monica Jun 26 '12 at 23:31
  • I have edited my question check it out! – ScarCode Jun 27 '12 at 03:42
  • 1
    @spyke Show us the calling code, or run `callgrind`. It is _obviously_ not your `paintEvent()` causing the lag as it's empty, but some other part of Qt that is being triggered by the painting mechanism. – cmannett85 Jun 27 '12 at 06:34

0 Answers0