QGridlayout - problem while adding multiple layouts



  • Hi

    I am working on a weather forecast widget and am trying to add the forecast details to a widget (not mainwindow) using the following code and it turns out as in the image.

     int nrow=0,ncol=0;
    
                for(int i=0;i< wWind.length();i++){
                    QLabel *wind = new QLabel(wWind[i]);
                    QLabel *time = new QLabel(wDates[i]);
                    QString imagePath = ":/Images/weatherIcons/" + wIcons[i];
                    QImage weatherIcon(imagePath);
                    QLabel *iconLabel = new QLabel();
                    iconLabel->setPixmap(QPixmap::fromImage(weatherIcon.scaled(64,64,Qt::KeepAspectRatio,Qt::FastTransformation)));
                    iconLabel->setToolTip(wDescription[i]);
                    QVBoxLayout *wdatalay = new QVBoxLayout();
                    wdatalay->addWidget(iconLabel);
                    wdatalay->addWidget(wind);
                    wdatalay->addWidget(time);
                    ui->gridLayout_2->addLayout(wdatalay,nrow,ncol,0);
                    ncol++;
                    if(ncol==3)
                    {
                        ncol =0;
                        nrow++;
                    }
    
    
                }// for loop ends
    

    The problem I am facing is that some part of the icon and one of the label are getting truncated. How to avoid this? Is it is due to the problem due to some constraint in row and column size?



  • addLayout(wdatalay,nrow,ncol,0); why are you setting rowspan to 0?

    You can set the minimum size explicitly iconLabel->setMinimumSize(QSize(64,64));



  • Thats not the rowspan. It is the Alignment.

    The default rowspan and columnspan is 1



  • The problem is the data is getting truncated as shown in the image. So if I want to display the data as it is by placing scrollbars, is it possible?



  • @saitej said in QGridlayout - problem while adding multiple layouts:

    So if I want to display the data as it is by placing scrollbars, is it possible?

    yes, just place your widget inside a scrollarea with setWidget()



  • @VRonin said in QGridlayout - problem while adding multiple layouts:

    scrollarea with setWidget()

    But I have a layout (wdatalay) which I need to add and there is no addlayout in scrollarea.

    Is it possible to group all the 3 labels as a widget ?



  • ui->gridLayout_2 is probably set on a widget, you need to set that widget to be child of the scrollarea



  • @VRonin

    Thanks !!

    I have added these lines but there is no change.

     QScrollArea *a = new QScrollArea(this);
        a->setWidget(ui->dummy);
        a->show();
    

    here, dummy is the widget ui->gridLayout_2 is set on



  • did you set the minimum size of the labels?

    btw, this works for me, try and adapt this example with your images:

    #include <QApplication>
    #include <QScrollArea>
    #include <QGridLayout>
    #include <QLabel>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc,argv);
        QScrollArea mainArea;
        QWidget* dummyWid = new QWidget(&mainArea);
        QGridLayout* mainLay = new QGridLayout(dummyWid);
        for (int row = 0; row < 30; ++row){
            for (int col = 0; col < 3; ++col) {
                QLabel* tempLabel = new QLabel("Label " + QString::number(row) + ',' + QString::number(col), dummyWid);
                mainLay->addWidget(tempLabel, row, col);
            }
        }
        mainArea.setWidget(dummyWid);
        mainArea.show();
        return a.exec();
    
    }
    

Log in to reply
 

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