Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. app using "excessive" CPU

app using "excessive" CPU

Scheduled Pinned Locked Moved Solved General and Desktop
42 Posts 10 Posters 8.0k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #32

    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?

    mrjjM J.HilkJ 2 Replies Last reply
    0
    • mzimmersM mzimmers

      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?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #33

      @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.

      1 Reply Last reply
      1
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by Bonnie
        #34

        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.

        1 Reply Last reply
        1
        • mzimmersM mzimmers

          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?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #35

          @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!


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #36

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

            1 Reply Last reply
            0
            • mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by mzimmers
              #37

              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)
              {
                  Q_UNUSED(event)
                  QPainter painter(this);
                  const QString filename(":/logos/CYBERDATA_IP_ENDPOINT_CO_small.png");
              
                  bool rc;
              
                  rc = m_pixmap.load(filename);
                  if (rc)
                  {
                      setPixmap(m_pixmap);
                      Qt::KeepAspectRatio, Qt::SmoothTransformation));
                      painter.drawPixmap(0, 0, *pixmap());
                  }
              }
              
              B 1 Reply Last reply
              0
              • O Offline
                O Offline
                ollarch
                wrote on last edited by
                #38

                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.

                1 Reply Last reply
                0
                • mzimmersM mzimmers

                  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)
                  {
                      Q_UNUSED(event)
                      QPainter painter(this);
                      const QString filename(":/logos/CYBERDATA_IP_ENDPOINT_CO_small.png");
                  
                      bool rc;
                  
                      rc = m_pixmap.load(filename);
                      if (rc)
                      {
                          setPixmap(m_pixmap);
                          Qt::KeepAspectRatio, Qt::SmoothTransformation));
                          painter.drawPixmap(0, 0, *pixmap());
                      }
                  }
                  
                  B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by Bonnie
                  #39

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

                  void LogoLabel::paintEvent(QPaintEvent *event)
                  {
                      Q_UNUSED(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.

                  label.setPixmap(QPixmap(":/logos/CYBERDATA_IP_ENDPOINT_CO_small.png"));
                  

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

                  void LogoLabel::resizeEvent(QResize*event)
                  {
                      QLabel::resizeEvent(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...

                  mzimmersM 1 Reply Last reply
                  4
                  • B Bonnie

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

                    void LogoLabel::paintEvent(QPaintEvent *event)
                    {
                        Q_UNUSED(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.

                    label.setPixmap(QPixmap(":/logos/CYBERDATA_IP_ENDPOINT_CO_small.png"));
                    

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

                    void LogoLabel::resizeEvent(QResize*event)
                    {
                        QLabel::resizeEvent(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...

                    mzimmersM Offline
                    mzimmersM Offline
                    mzimmers
                    wrote on last edited by
                    #40

                    @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.

                    B jsulmJ 2 Replies Last reply
                    0
                    • mzimmersM mzimmers

                      @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.

                      B Offline
                      B Offline
                      Bonnie
                      wrote on last edited by
                      #41

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

                      1 Reply Last reply
                      5
                      • mzimmersM mzimmers

                        @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.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #42

                        @mzimmers To add to @Bonnie it is actually explained in the documentation https://doc.qt.io/qt-5/qpainter.html :
                        "Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent()."

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        3

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved