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. Crash when add to many QWidgets to QVector
Forum Update on Monday, May 27th 2025

Crash when add to many QWidgets to QVector

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 832 Views
  • 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.
  • T Offline
    T Offline
    TomNow99
    wrote on last edited by TomNow99
    #1

    Hello,

    My code ( whole ):

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        wek.reserve(90*90);
    
        for(int i=0;i<90*90;i++)
        {
            wek.append(new QLabel(this));
        }
    }
    

    wek is QVector<QLabel *>

    When I change 90x90 to 80x80 everything is ok.

    I check that sizeof(QLabel) is 48, so I need 48x90x90 bytes < 1 mb memory. My pc has 8 gb ram ( 3.7 gb is free ).

    Using windows 10, Qt 5.14.0, MinGW 64 bit

    jsulmJ JonBJ 2 Replies Last reply
    0
    • T TomNow99

      Hello,

      My code ( whole ):

      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          wek.reserve(90*90);
      
          for(int i=0;i<90*90;i++)
          {
              wek.append(new QLabel(this));
          }
      }
      

      wek is QVector<QLabel *>

      When I change 90x90 to 80x80 everything is ok.

      I check that sizeof(QLabel) is 48, so I need 48x90x90 bytes < 1 mb memory. My pc has 8 gb ram ( 3.7 gb is free ).

      Using windows 10, Qt 5.14.0, MinGW 64 bit

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

      @TomNow99 What type of crash is it? Do you have stack trace after crash?

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

      1 Reply Last reply
      0
      • T TomNow99

        Hello,

        My code ( whole ):

        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            wek.reserve(90*90);
        
            for(int i=0;i<90*90;i++)
            {
                wek.append(new QLabel(this));
            }
        }
        

        wek is QVector<QLabel *>

        When I change 90x90 to 80x80 everything is ok.

        I check that sizeof(QLabel) is 48, so I need 48x90x90 bytes < 1 mb memory. My pc has 8 gb ram ( 3.7 gb is free ).

        Using windows 10, Qt 5.14.0, MinGW 64 bit

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @TomNow99
        After you have answered @jsulm .
        If there is indeed a crash, if anything it's probably more likely for the number of widgets to show than putting them in a vector. What happens if you remove the parent: wek.append(new QLabel());?

        P.S.
        Don't create anything near 10,000 widgets, regardless!

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TomNow99
          wrote on last edited by
          #4

          @jsulm SIGSEGV @JonB
          screen.png

          When I remove parent - everything is ok

          jsulmJ 1 Reply Last reply
          0
          • T TomNow99

            @jsulm SIGSEGV @JonB
            screen.png

            When I remove parent - everything is ok

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

            @TomNow99 Looks like it is crashing when drawing a widget. Probably Qt does not like many widgets to be added that fast :-) I suggest to file a bug in Qt bug tracker as there should not be any crashes just because you add many widgets in a loop.

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

            1 Reply Last reply
            1
            • T Offline
              T Offline
              TomNow99
              wrote on last edited by TomNow99
              #6

              @jsulm @JonB

              I had a problem ( and still have with adding many widgets to grid / print them on the screen ):

              https://forum.qt.io/topic/118875/is-there-a-maximum-number-of-qlabels-in-a-screen/2

              In this topic I see that user J.Hilk doesn't have a problem with adding many labels very fast. Maybe because of OS ( my Win 10 vs his linux )?

              EDIT: I try add many QLabels to QGridLayout ( 120 x 120 ) on linux ( VirtualBox - Fedora ) and it was no problem ( I had to wait a moment, but there are ). So do you think OS is a problem?

              Can someone check and agree with me that in Win 10 I can't add many labels?

              JonBJ 1 Reply Last reply
              0
              • T TomNow99

                @jsulm @JonB

                I had a problem ( and still have with adding many widgets to grid / print them on the screen ):

                https://forum.qt.io/topic/118875/is-there-a-maximum-number-of-qlabels-in-a-screen/2

                In this topic I see that user J.Hilk doesn't have a problem with adding many labels very fast. Maybe because of OS ( my Win 10 vs his linux )?

                EDIT: I try add many QLabels to QGridLayout ( 120 x 120 ) on linux ( VirtualBox - Fedora ) and it was no problem ( I had to wait a moment, but there are ). So do you think OS is a problem?

                Can someone check and agree with me that in Win 10 I can't add many labels?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @TomNow99
                I can't test for you, but:

                I try add many QLabels to QGridLayout ( 120 x 120 )

                That's 10,000+ labels, and totally the wrong thing to do, as I wrote earlier. Regardless of whether there is any bug on whatever OS, you really, really should not ignore this! You want to change something like this to use, say, a table and a QStyledItemDelegate with the labels/text drawn rather than as actual Qt widgets....

                [EDIT I see my learned colleague @VRonin has up-voted this post. As a campaigner for QStyledItemDelegate instead of individual widgets, I'm sure he would be happy to help out if you have any questions about this approach ;-) ]

                1 Reply Last reply
                3
                • T Offline
                  T Offline
                  TomNow99
                  wrote on last edited by
                  #8

                  @JonB I agree with you, but... what if I would like to have 10000 signals and slots to this qlabels? Of course I can check mouse click position, but there is many math.

                  jsulmJ JonBJ 2 Replies Last reply
                  0
                  • T TomNow99

                    @JonB I agree with you, but... what if I would like to have 10000 signals and slots to this qlabels? Of course I can check mouse click position, but there is many math.

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

                    @TomNow99 What is your use case? Why do you need that many labels?!

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

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      TomNow99
                      wrote on last edited by
                      #10

                      @jsulm For example: when I was in a College I had exercise with machine learning. I needed many labels ( they shows image ( 1 pixel = 1 label ) ), which I can click and change their color ( black - white ).

                      1 Reply Last reply
                      0
                      • T TomNow99

                        @JonB I agree with you, but... what if I would like to have 10000 signals and slots to this qlabels? Of course I can check mouse click position, but there is many math.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @TomNow99 said in Crash when add to many QWidgets to QVector:

                        I would like to have 10000 signals and slots to this qlabels? Of course I can check mouse click position, but there is many math.

                        Nobody wants 10,000 signals & slots. Honest! One signal, one slot, one parameter saying where the click is. Yes, if that's the choice, do the math (and btw it sounds like two divide operations) :)

                        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