0
votes

I'm having some issues working with the QGridLayout. Here's the code and the explanation comes after:

for(int i =0; i<fileCount; i++)
{
    int row = 0;
    int col = 0;
    QString DocName = FilteredFiles.at(i).at(0);
    QLabel* DocTitle = new QLabel;
    DocTitle->setText(DocName);
    QLabel* DocIcon = new QLabel;
    if(FilteredFiles.at(i).at(2)== "WORD")
    {
        QPixmap Icon("C:blah/blah/blah/WordIcon.jpg");
        DocIcon->setPixmap(Icon);
    }
    else if(FilteredFiles.at(i).at(2)== "EXCEL")
    {
        QPixmap Icon("C:/blah/blah/blah/ExcelIcon.png");
        DocIcon->setPixmap(Icon);
    }
    else
    {
        QPixmap Icon("C:/blah/blah/blah/PpIcon.png");
        DocIcon->setPixmap(Icon);
    }
    GridContainer->addWidget(DocIcon);
    GridContainer->addWidget(DocTitle);
    TopGrid->addLayout(GridContainer,row,col,1,1);
    col++;
}
MainContainer->addLayout(TopGrid);

The above code is supposed to make two Qlabels, pixmap an image to one of the labels, add the pixmapped label and the regular label to a QVBoxLayout and then add the QVBoxLayout to a QGridLayout. The output grid was supposed to have everything on a single row but it's in a column. Can someone explain why this is happening?

enter image description here

2

2 Answers

1
votes

I'm assuming GridContainer is a QVBoxLayout. You're adding all your widgets into the same layout. That's probably your problem (as well as the problem with your col variable being initialized inside your for loop, which was pointed out by user2672165).

You should probably be creating a new GridContainer object inside your loop and adding that to your TopGrid layout, instead of adding the same layout in your TopGrid layout over and over again.

int col = 0;
int row = 0;
for(int i =0; i<fileCount; i++)
{
    GridContainer = new QVBoxLayout; // create a new layout
    QString DocName = FilteredFiles.at(i).at(0);
    QLabel* DocTitle = new QLabel;
    DocTitle->setText(DocName);
    QLabel* DocIcon = new QLabel;
    if(FilteredFiles.at(i).at(2)== "WORD")
    {
        QPixmap Icon("C:blah/blah/blah/WordIcon.jpg");
        DocIcon->setPixmap(Icon);
    }
    else if(FilteredFiles.at(i).at(2)== "EXCEL")
    {
        QPixmap Icon("C:/blah/blah/blah/ExcelIcon.png");
        DocIcon->setPixmap(Icon);
    }
    else
    {
        QPixmap Icon("C:/blah/blah/blah/PpIcon.png");
        DocIcon->setPixmap(Icon);
    }
    GridContainer->addWidget(DocIcon);
    GridContainer->addWidget(DocTitle);
    TopGrid->addLayout(GridContainer,row,col,1,1);
    col++;
}
MainContainer->addLayout(TopGrid);
1
votes

Move variable col out of loop:

int col = 0;
for(int i =0; i<fileCount; i++)
{
    int row = 0;
    QString DocName = FilteredFiles.at(i).at(0);
    QLabel* DocTitle = new QLabel;
    DocTitle->setText(DocName);
    QLabel* DocIcon = new QLabel;
    if(FilteredFiles.at(i).at(2)== "WORD")
    {
        QPixmap Icon("C:blah/blah/blah/WordIcon.jpg");
        DocIcon->setPixmap(Icon);
    }
    else if(FilteredFiles.at(i).at(2)== "EXCEL")
    {
        QPixmap Icon("C:/blah/blah/blah/ExcelIcon.png");
        DocIcon->setPixmap(Icon);
    }
    else
    {
        QPixmap Icon("C:/blah/blah/blah/PpIcon.png");
        DocIcon->setPixmap(Icon);
    }
    GridContainer->addWidget(DocIcon);
    GridContainer->addWidget(DocTitle);
    TopGrid->addLayout(GridContainer,row,col,1,1);
    col++;
}
MainContainer->addLayout(TopGrid);