Problem with number of lines in a layout

  • I am doing a simple program. Basically it reads a image and show how many different colors have the image. For each color it is added a QLabel and a QPushButton in a QHBoxLayout. This QHBoxLayout it is added to a QVBoxLayout. Though, when there are more than 234 colors the layout freezes. I replaced this several layouts by a GridLayout and the problems remains.
    A curious fact is that a single column more than 234 QLabels doesn't generate any problem, but a single column with more than 234 QPushButton does generate.
    Follow a brief excerpt of the code region where the problem happens:

    for(int index = 0; index < numColors; index++)
    //            if( index == 230 ) return;
                QLabel *rgbLabel = new QLabel(QString("Color %1").arg(index));
                QPushButton *colorButton = new QPushButton( "..." );
                ui->gridLayoutColors->addWidget(rgbLabel, index, 0);
                ui->gridLayoutColors->addWidget(colorButton, index, 1);

    And so does it follows a screenshot with the problem

    I forgot to mention my environment:

    • Ubuntu 14.04 LTS 64 bits (4 GB RAM)
    • i3 2 core,
    • Qt 4.8.6
    • gcc 4.8

    Does someone know why this problem is happening? How I can circumvent it?

    Obs: I have tested the project on the same machine but in a Win7 64 bits with Qt 4.8.6 + MSV2013 that I also have installed and everything worked OK. The test was done with a image containing 300 different colors. I believe it is a bug/limitation of Qt 4.8.6 for Linux.

    Thanks beforehand.

  • Lifetime Qt Champion


    Not an answer to your question but since your images number of colors might get way higher than that, shouldn't your rather use a QListView/QTableView + QStandardItemModel or a QListWidget/QTableWidget to show the text and maybe a custom QStyledItemDelegate to show the button when appropriate ?

  • It was what I did.

    But I confess that I didn't hope that an error like that would happen. Though I imagine that it is due to it was not designed to a great number of child widgets, what makes sense.

  • You should use a QListView/QTableView for this kind of things but just for the sake of it, this code runs fine for me (Qt 5.5 Windows 7)

    #include <QApplication>
    #include <QWidget>
    #include <QDebug>
    #include <QGridLayout>
    #include <QPushButton>
    #include <QLabel>
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        QWidget w;
        QGridLayout* gridLayoutColors=new QGridLayout(&w);
        for(int index = 0; index < 250; index++){
            QLabel *rgbLabel = new QLabel(QString("Color %1").arg(index),&w);
            QPushButton *colorButton = new QPushButton( "...",&w );
            gridLayoutColors->addWidget(rgbLabel, index, 0);
            gridLayoutColors->addWidget(colorButton, index, 1);
        return a.exec();

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.