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. QWidget paintEvent slow when rendering
Forum Updated to NodeBB v4.3 + New Features

QWidget paintEvent slow when rendering

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 3 Posters 9.0k Views 2 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.
  • D Dohisev

    @mrjj That is actually a default gui with nothing more than this sidebar promoted to a widget and a QTableWidget.
    I don't know what is happening, as I said, if I remove the code to draw the text it doesn't blink.

    Precisely this part of the code:

    QString title = "Welcome";
    
    QSize size = painter.fontMetrics().size(
        Qt::TextSingleLine, title
    );
    

    If I comment that part of the code this is how it runs:

    0_1510166324835_2017-11-08_15-38-19.gif

    D Offline
    D Offline
    Dohisev
    wrote on last edited by
    #8

    Anyone to help me with that?

    J.HilkJ 1 Reply Last reply
    0
    • D Dohisev

      Anyone to help me with that?

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

      @Dohisev
      mmh, have you tried calling the base paintEvent first?

      void Sidebar::paintEvent(QPaintEvent *event)
      {
          QWidget::paintEvent(event);
      
          QPainter painter(this);
      
          ...
      }
      

      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.

      D 1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #10

        @Dohisev

        I would guess on first call to fontMetrics is expensive as it reads font info.

        1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @Dohisev
          mmh, have you tried calling the base paintEvent first?

          void Sidebar::paintEvent(QPaintEvent *event)
          {
              QWidget::paintEvent(event);
          
              QPainter painter(this);
          
              ...
          }
          
          D Offline
          D Offline
          Dohisev
          wrote on last edited by
          #11

          @J.Hilk It's still the same, unfortunately.
          @mrjj Do you have any idea of what I can do to solve this?

          mrjjM 1 Reply Last reply
          0
          • D Dohisev

            @J.Hilk It's still the same, unfortunately.
            @mrjj Do you have any idea of what I can do to solve this?

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

            @Dohisev

            Yes call fontMetrics outside the paint and calc size there to see if that works faster.
            make sur eto use same font both places :)

            QFont font("times", 24);
            QFontMetrics fm(font);
            int pixelsWide = fm.width("What's the width of this text?");
            

            Normally its not that slow. so u might have a broken ttf file or something.

            D 1 Reply Last reply
            0
            • D Offline
              D Offline
              Dohisev
              wrote on last edited by
              #13
              This post is deleted!
              1 Reply Last reply
              0
              • mrjjM mrjj

                @Dohisev

                Yes call fontMetrics outside the paint and calc size there to see if that works faster.
                make sur eto use same font both places :)

                QFont font("times", 24);
                QFontMetrics fm(font);
                int pixelsWide = fm.width("What's the width of this text?");
                

                Normally its not that slow. so u might have a broken ttf file or something.

                D Offline
                D Offline
                Dohisev
                wrote on last edited by Dohisev
                #14

                @mrjj I tried this but it doesn't work. I'm even drawing icons on the sidebar and it's not freezing, but when I uncomment the draw text it does freeze.

                edit: It's not the font metrics, it's the drawText itself, I put only: painter.drawText(itemRect, Qt::AlignCenter, "welcome"); and it's freezing.

                mrjjM 1 Reply Last reply
                0
                • D Dohisev

                  @mrjj I tried this but it doesn't work. I'm even drawing icons on the sidebar and it's not freezing, but when I uncomment the draw text it does freeze.

                  edit: It's not the font metrics, it's the drawText itself, I put only: painter.drawText(itemRect, Qt::AlignCenter, "welcome"); and it's freezing.

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

                  @Dohisev
                  I have not seen that before. Do you set a special font or something?
                  Does any of the many samples do anything like that?
                  Did you install extra fonts to windows?

                  D 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @Dohisev
                    I have not seen that before. Do you set a special font or something?
                    Does any of the many samples do anything like that?
                    Did you install extra fonts to windows?

                    D Offline
                    D Offline
                    Dohisev
                    wrote on last edited by
                    #16

                    @mrjj Yes, it doesn't happen with all examples on Qt using QPainter but probably does happen when using to draw a text. Yes, I have installed fonts on Windows but how does that changes anything? No, I'm not using a font to draw, I'm just calling to draw a text and that is it.

                    mrjjM 1 Reply Last reply
                    0
                    • D Dohisev

                      @mrjj Yes, it doesn't happen with all examples on Qt using QPainter but probably does happen when using to draw a text. Yes, I have installed fonts on Windows but how does that changes anything? No, I'm not using a font to draw, I'm just calling to draw a text and that is it.

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

                      @Dohisev
                      Qt will enumerate all fonts. So if some of the ttf files are bad, it can hang a bit in that.
                      On a desktop class system, normally its shown instantly so
                      the reason that drawText is slow must be found somewhere else.

                      How high resolution are you running?

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        Dohisev
                        wrote on last edited by
                        #18

                        What you mean with the resolution? You mean my screen resolution? It's 1366x768.

                        Is there a workaround to check if it's a problem with the enumeration Qt does with the fonts?

                        mrjjM 1 Reply Last reply
                        0
                        • D Dohisev

                          What you mean with the resolution? You mean my screen resolution? It's 1366x768.

                          Is there a workaround to check if it's a problem with the enumeration Qt does with the fonts?

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

                          @Dohisev
                          Hi
                          It also have a small delay here. So maybe there is nothing wrong.
                          alt text

                          D 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @Dohisev
                            Hi
                            It also have a small delay here. So maybe there is nothing wrong.
                            alt text

                            D Offline
                            D Offline
                            Dohisev
                            wrote on last edited by
                            #20

                            @mrjj I really can't notice the delay on yours, on mine it's like way too much.

                            https://ddgobkiprc33d.cloudfront.net/57399f19-427b-4f7f-b48b-064649ae1a22.gif

                            Focus on the mouse cursor and see that it appears a loading cursor while it's all white.

                            1 Reply Last reply
                            0
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #21

                              Yeah your first paint for mainwindow seems much slower.

                              But i dont get it if none of the other samples do anything like that as they are have same structure etc.

                              Can you try my test project?
                              https://www.dropbox.com/s/nl0issoplwylkb7/slowtext.zip?dl=0

                              Just to verify that it runs slower at your pc. Then it must be local somehow.,

                              D 1 Reply Last reply
                              0
                              • mrjjM mrjj

                                Yeah your first paint for mainwindow seems much slower.

                                But i dont get it if none of the other samples do anything like that as they are have same structure etc.

                                Can you try my test project?
                                https://www.dropbox.com/s/nl0issoplwylkb7/slowtext.zip?dl=0

                                Just to verify that it runs slower at your pc. Then it must be local somehow.,

                                D Offline
                                D Offline
                                Dohisev
                                wrote on last edited by
                                #22

                                @mrjj It seems to be a local problem, I don't know if this will be happening if I compile here and distribute the generated binary. What should I do?

                                0_1510246550049_2017-11-09_13-54-10.gif

                                mrjjM 1 Reply Last reply
                                0
                                • D Dohisev

                                  @mrjj It seems to be a local problem, I don't know if this will be happening if I compile here and distribute the generated binary. What should I do?

                                  0_1510246550049_2017-11-09_13-54-10.gif

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

                                  @Dohisev
                                  First i would update video drivers. just to be on the safe side.
                                  Im pretty sure it wont happen when you distribute your app but
                                  it would be nice to know what it comes from.

                                  If you remove the drawText , the speed is normal ?

                                  D 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @Dohisev
                                    First i would update video drivers. just to be on the safe side.
                                    Im pretty sure it wont happen when you distribute your app but
                                    it would be nice to know what it comes from.

                                    If you remove the drawText , the speed is normal ?

                                    D Offline
                                    D Offline
                                    Dohisev
                                    wrote on last edited by
                                    #24

                                    @mrjj Yes, it's only slow when drawing the texts, If I draw only shapes it doesn't lag.

                                    mrjjM 1 Reply Last reply
                                    0
                                    • D Dohisev

                                      @mrjj Yes, it's only slow when drawing the texts, If I draw only shapes it doesn't lag.

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

                                      @Dohisev
                                      You could try remove some of the new fonts from the font folder and see.

                                      D 1 Reply Last reply
                                      0
                                      • mrjjM mrjj

                                        @Dohisev
                                        You could try remove some of the new fonts from the font folder and see.

                                        D Offline
                                        D Offline
                                        Dohisev
                                        wrote on last edited by
                                        #26

                                        @mrjj I did remove all the fonts I installed but it's still the same, so I'm going to be updating the drivers and hope that everything is back to normal after that, if not, I think it might be something with my computer. It might be needing a clean up or something like that.
                                        Thank you mrjj, you helped a lot, thank you for your patience.

                                        mrjjM 1 Reply Last reply
                                        0
                                        • D Dohisev

                                          @mrjj I did remove all the fonts I installed but it's still the same, so I'm going to be updating the drivers and hope that everything is back to normal after that, if not, I think it might be something with my computer. It might be needing a clean up or something like that.
                                          Thank you mrjj, you helped a lot, thank you for your patience.

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

                                          @Dohisev
                                          You are most welcome.
                                          It is a bit of odd case and updating gfx drivers is a good idea.
                                          Its clearly related to drawText but i think its something deeper that Qt.

                                          D 1 Reply Last reply
                                          0

                                          • Login

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