Important: Please read the Qt Code of Conduct -

app using "excessive" CPU

  • @mzimmers said in app using "excessive" CPU:

    And, if you're confident this won't happen on Linux, then maybe the problem isn't in my code space...

    Noooo, I put a wink --> ;-) <--
    I absolutely do not know whether it will repro under Linux, if you're lucky it will, I just meant sod's law it won't!

  • Lifetime Qt Champion

    That stack trace is for

    1. starting app
      2: add break point
      3: let it loose focus to see the issue

    and not just set at startup so we ssee the first paint when it becomes visible?
    Just asking to be sure. Not seeing anything special besides maybe the sendSpontaneousEvent

  • @mrjj I'm not sure I follow you, but I modified my routine:

    void Widget::paintEvent(QPaintEvent *event)
        static int count = 0;
        if (event->spontaneous())
            //qDebug() << "spontaneous event" << count++;

    I put a breakpoint on QWidget::paintEvent...and it never, EVER hits. (Also, with this change, CPU usage remains the same.)

    This just gets weirder and weirder.

  • I am sorry this is such a struggle. I don't have anything more to add except a sarcastic example of wiggling:

  • Lifetime Qt Champion

    hehe that the sorts you want to debug with a flame thrower...

  • Lifetime Qt Champion


    well i just asked if it was not as in first run stack trace.
    but i think you are doing as i think reading your last post.

    Its very odd. Indeed.
    //qDebug() << "spontaneous event" << count++;
    trigger alow when testing then ?

  • @mrjj I just realized something -- from the docs:

    bool QEvent::spontaneous() const
    Returns true if the event originated outside the application (a system event); otherwise returns false.
    The return value of this function is not defined for paint events.

    So, I think this exercise was a waste of time.

  • Lifetime Qt Champion


    Well i find it very odd you see many paint events for the widget (with event filter) but
    its not constantly Hitting the break point in paintEvent. ??
    Or did i misunderstood something ?

  • @mrjj you understand it perfectly, and "odd" is a mild term for it.

    I seriously don't know what to look at here. I'm afraid that the profiler we run tomorrow won't show anything in my code space.

  • Lifetime Qt Champion

    Do you have any timers or threads ?
    Something scanning for those devices you show ?

    Also, just for test. a plain normal GUI project with say a ListWidget on it does not show this
    cpu usage, right?

    It must somehow be related to your code ?

  • @mzimmers said in app using "excessive" CPU:

    I'm afraid that the profiler we run tomorrow won't show anything in my code space.

    Always with the negative waves Have a little faith, baby!

  • Well, my associate hasn't yet built the app on Linux, but I discovered the source (if not the cause) of the problem: my logo.

    I have a small (201x59 pixel) PNG in the upper left of my widget. When I remove it from my .qrc file, the CPU usage disappears.

    Any ideas on this one?

  • @mzimmers said in app using "excessive" CPU:

    my logo

    Wow! That is all I can say.

  • I suspect it has something to do with this line:

    painter.drawPixmap(rect(), pixmap()->scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));

    I don't need to scale it; what do I replace the call to scaled() with?

  • Lifetime Qt Champion

    @mzimmers said in app using "excessive" CPU:

    painter.drawPixmap(rect(), pixmap()->scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));

    painter.drawPixmap(rect(), pixmap());
    actaully scale it to rect so @Bonnie version is correct.

    to draw it at original size.

    or you can simply scale it once outside paintEvent and reuse the scaled version.

  • To not scale at all, should not use a rect as parameter (unless the rect size is the same as the pixmap size).

    painter.drawPixmap(0, 0, pixmap());

    (0, 0) is the draw position.

  • Moderators

    @mzimmers great you found it! But paintEvent itself should not be called that much regularly! I suspect your logo is being resized (wiggles) all the time!

  • @mzimmers
    Ah ha! We have been looking for where you "wiggle", I suspect @J-Hilk has found it!?

  • Thank you all for the replies. I tried mrjj's/Bonnie's suggestions, and they didn't change the CPU usage.

    But really, the question (IMO) is WHY is my logo being wiggled? I have matched the dimensions of the .png file to the QWidget (a QLabel) that displays it. I've set the QLabel's size to fixed. What could be causing this flood of paint events?

    The technique I'm using is to promote the QLabel to a class I've created (LogoLabel). Here's the entirety of its paint event:

    void LogoLabel::paintEvent(QPaintEvent *event)
        QPainter painter(this);
        const QString filename(":/logos/CYBERDATA_IP_ENDPOINT_CO_small.png");
        bool rc;
        rc = m_pixmap.load(filename);
        if (rc)
            Qt::KeepAspectRatio, Qt::SmoothTransformation));
            painter.drawPixmap(0, 0, *pixmap());

  • Hi,

    You could reimplement "reseizeEvent" and do the Pixmap scale there. On "paintEvent", just paint it or just set the pixmap to the Label and it will paint it.

  • @mzimmers Why are you doing the loading in the paintEvent???

    void LogoLabel::paintEvent(QPaintEvent *event)
         //m_pixmap should already be loaded
        if (!m_pixmap.isNull())
            QPainter painter(this);
            painter.drawPixmap(0, 0, m_pixmap);

    I remembered you had a post said you write your label class because you want the QLabel to scale smoother.
    Now, if you do not need to scale, you only need to load the pixmap once after it is created.


    If you still need to scale, I think you should scale it in the resizeEvent.

    void LogoLabel::resizeEvent(QResize*event)
        if(QPixmap *pix = pixmap())
            m_pixmap = pix->scaled(event->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);

    Actually, I don't think you need to subclass QLabel...Subclassing QWidget is enough if you reimplement all these events...

  • @Bonnie you nailed it. I moved the load to the c'tor, and everything works much better now.

    A couple of notes:

    1. I turned on my event filter, and it's no longer spewing zillions of events, so I guess my keyPress idea would have worked (if Windows wasn't so lame).
    2. I tried making my painter a member variable and initializing it in the c'tor, but that didn't work. Any idea why?

    Thanks to everyone who looked at this.

  • @mzimmers
    You can only create and use a painter of a widget in its paintEvent.

  • Lifetime Qt Champion

    @mzimmers To add to @Bonnie it is actually explained in the documentation :
    "Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent()."

Log in to reply