Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Crash when add to many QWidgets to QVector



  • 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


  • Lifetime Qt Champion

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



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



  • @jsulm SIGSEGV @JonB
    screen.png

    When I remove parent - everything is ok


  • Lifetime Qt Champion

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



  • @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?



  • @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 ;-) ]



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


  • Lifetime Qt Champion

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



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



  • @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) :)


Log in to reply