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 Offline
    D Offline
    Dohisev
    wrote on 8 Nov 2017, 14:11 last edited by
    #1

    I'm creating a kind of sidebar and so I created a class called Sidebar and inside the paintEvent I started drawing this to test how I'm going to be drawing the sidebar dynamically later, I tried to draw a kind of "button" but I noticed that it takes a while to render while loading the sidebar in the meantime it's all white.

    void Sidebar::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
    
        // paint the background
        painter.fillRect(rect(), QColor(100, 100, 100));
    
        // draw the sidebar button
        QRect buttonRect(0, 0, 80, 80);
        painter.fillRect(buttonRect, QColor(150, 150, 150));
    
        // draw the title
        QString title = "Welcome";
    
        QSize size = painter.fontMetrics().size(
            Qt::TextSingleLine, title
        );
    
        QRect textRect(
            QPoint(
                buttonRect.width() / 2 - size.width() / 2,
                buttonRect.bottom() - size.height() - 5
            ),
            size
        );
    
        painter.drawText(textRect, Qt::AlignCenter, title);
    }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 8 Nov 2017, 16:57 last edited by
      #2

      Hi
      That code do not seem to do a lot so if you see it as white my
      guess is that you use a loop and block the event queue while loading/do other stuff and
      it never get the change to call paintEvent before later.

      D 1 Reply Last reply 8 Nov 2017, 18:13
      0
      • M mrjj
        8 Nov 2017, 16:57

        Hi
        That code do not seem to do a lot so if you see it as white my
        guess is that you use a loop and block the event queue while loading/do other stuff and
        it never get the change to call paintEvent before later.

        D Offline
        D Offline
        Dohisev
        wrote on 8 Nov 2017, 18:13 last edited by Dohisev 11 Aug 2017, 18:14
        #3

        @mrjj I'm not calling anything other than that. I have promoted that Sidebar widget to a widget that is in a layout and nothing more than that.

        I feel it kinda "lagging" when loading, it like freezes the gui for a few microseconds like everything is white like frozen and then it just blinks everything and appears.
        All the examples using QPainter doesn't have this problem when loading but I'm having.

        And just to be clear: I doesn't be white forever, it does work after, I'm saying that it freezes or something like that before appearing.

        M 1 Reply Last reply 8 Nov 2017, 18:22
        0
        • D Dohisev
          8 Nov 2017, 18:13

          @mrjj I'm not calling anything other than that. I have promoted that Sidebar widget to a widget that is in a layout and nothing more than that.

          I feel it kinda "lagging" when loading, it like freezes the gui for a few microseconds like everything is white like frozen and then it just blinks everything and appears.
          All the examples using QPainter doesn't have this problem when loading but I'm having.

          And just to be clear: I doesn't be white forever, it does work after, I'm saying that it freezes or something like that before appearing.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 8 Nov 2017, 18:22 last edited by
          #4

          @Dohisev
          What device are you running on ?

          So you just have a mainwindow and have a widget u promoted to
          Sidebar ?
          And side bar does not do anything beside the little drawing u have shown?

          D 1 Reply Last reply 8 Nov 2017, 18:28
          0
          • M mrjj
            8 Nov 2017, 18:22

            @Dohisev
            What device are you running on ?

            So you just have a mainwindow and have a widget u promoted to
            Sidebar ?
            And side bar does not do anything beside the little drawing u have shown?

            D Offline
            D Offline
            Dohisev
            wrote on 8 Nov 2017, 18:28 last edited by
            #5

            @mrjj I'm on Windows x64 bit, Qt 5.9.2, MSVC 2015, Intel i5.

            Noticed that it starts all white and then blinks everything on the screen? It's like freezing the gui until it paints.
            I removed a few parts from the code and I noticed that it happens when drawing the text.

            0_1510165653582_2017-11-08_15-25-03.gif

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 8 Nov 2017, 18:33 last edited by
              #6

              Hi
              Ok, that should not lag at all.
              That is a bit odd as promotion is nothing
              fancy and basically, it does
              SideBar *var= new SideBar() for you

              Is this when u start as debug or just run?

              Does a default empty GUI app, with a few widgets also does this blink in ?

              D 1 Reply Last reply 8 Nov 2017, 18:38
              0
              • M mrjj
                8 Nov 2017, 18:33

                Hi
                Ok, that should not lag at all.
                That is a bit odd as promotion is nothing
                fancy and basically, it does
                SideBar *var= new SideBar() for you

                Is this when u start as debug or just run?

                Does a default empty GUI app, with a few widgets also does this blink in ?

                D Offline
                D Offline
                Dohisev
                wrote on 8 Nov 2017, 18:38 last edited by
                #7

                @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 1 Reply Last reply 9 Nov 2017, 07:10
                0
                • D Dohisev
                  8 Nov 2017, 18:38

                  @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 9 Nov 2017, 07:10 last edited by
                  #8

                  Anyone to help me with that?

                  J.HilkJ 1 Reply Last reply 9 Nov 2017, 07:20
                  0
                  • D Dohisev
                    9 Nov 2017, 07:10

                    Anyone to help me with that?

                    J.HilkJ Online
                    J.HilkJ Online
                    J.Hilk
                    Moderators
                    wrote on 9 Nov 2017, 07:20 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 9 Nov 2017, 13:10
                    0
                    • M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 9 Nov 2017, 07:42 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
                        9 Nov 2017, 07:20

                        @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 9 Nov 2017, 13:10 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?

                        M 1 Reply Last reply 9 Nov 2017, 13:15
                        0
                        • D Dohisev
                          9 Nov 2017, 13:10

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

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 9 Nov 2017, 13:15 last edited by mrjj 11 Sept 2017, 13:15
                          #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 9 Nov 2017, 13:56
                          0
                          • D Offline
                            D Offline
                            Dohisev
                            wrote on 9 Nov 2017, 13:30 last edited by
                            #13
                            This post is deleted!
                            1 Reply Last reply
                            0
                            • M mrjj
                              9 Nov 2017, 13:15

                              @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 9 Nov 2017, 13:56 last edited by Dohisev 11 Sept 2017, 14:00
                              #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.

                              M 1 Reply Last reply 9 Nov 2017, 16:07
                              0
                              • D Dohisev
                                9 Nov 2017, 13:56

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

                                M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 9 Nov 2017, 16:07 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 9 Nov 2017, 16:12
                                0
                                • M mrjj
                                  9 Nov 2017, 16:07

                                  @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 9 Nov 2017, 16:12 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.

                                  M 1 Reply Last reply 9 Nov 2017, 16:14
                                  0
                                  • D Dohisev
                                    9 Nov 2017, 16:12

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

                                    M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 9 Nov 2017, 16:14 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 9 Nov 2017, 16:24 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?

                                      M 1 Reply Last reply 9 Nov 2017, 16:26
                                      0
                                      • D Dohisev
                                        9 Nov 2017, 16:24

                                        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?

                                        M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 9 Nov 2017, 16:26 last edited by mrjj 11 Sept 2017, 16:27
                                        #19

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

                                        D 1 Reply Last reply 9 Nov 2017, 16:29
                                        0
                                        • M mrjj
                                          9 Nov 2017, 16:26

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

                                          D Offline
                                          D Offline
                                          Dohisev
                                          wrote on 9 Nov 2017, 16:29 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

                                          1/29

                                          8 Nov 2017, 14:11

                                          • Login

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