0

我在使用 QGridLayout 时遇到了一些问题。这是代码,后面是解释:

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);

上面的代码应该创建两个 Qlabel,将图像像素映射到其中一个标签,将像素映射标签和常规标签添加到 QVBoxLayout,然后将 QVBoxLayout 添加到 QGridLayout。输出网格应该在一行中包含所有内容,但它在列中。有人可以解释为什么会这样吗?

在此处输入图像描述

4

2 回答 2

1

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);
于 2013-09-25T07:37:50.007 回答
1

将变量 col 移出循环:

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);
于 2013-09-24T17:43:41.717 回答