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();
                    QVBoxLayout *wdatalay = new QVBoxLayout();
                        ncol =0;
                }// 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?

  • Qt Champions 2018

    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?

  • Qt Champions 2018

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

  • Qt Champions 2018

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

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

  • Qt Champions 2018

    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);
        return a.exec();

Log in to reply

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