Widgets in QGridlayout



  • Hi,

    i am using QGridlayout to show QLabel and QWidget,

    in a loop

    
    int m_layout  = 4
    
    for(int i = 0 ; i < m_layout ; i++ )
        {
            m_List[i].m_label = new QLabel;
            m_List[i].m_label ->setText("Label");
    
            m_List[i].widget = new QWidget;
            m_List[i].widget->setStyleSheet("background-color:blue");
          
            m_grid->addWidget(m_List[i].m_label,row,col);
            m_grid->addWidget(m_List[i].widget,row,col);
    }
        m_vbox->addLayout(m_grid);
    

    but it is not coming one after the other in vertical fashion, it is overlaying, how to provide to make label and widget appear vertically in QGridLayout

    Thanks,



  • You are not incrementing your 'row' and/or 'col' variable at the end of your loop.



  • I am updating at the end of the loop, the problem is i want

       m_List[i].m_label = new QLabel;
        m_List[i].m_label ->setText("Label");
    
        m_List[i].widget = new QWidget;
        m_List[i].widget->setStyleSheet("background-color:blue");
    

    to be in vertical aligned in each cell of the grid.

    in the link below , only widget is appearing, i want to have label at the top of widget in each cell

    https://postimg.org/image/huzoyoxw7/

    Thanks,


  • Lifetime Qt Champion

    Hi,

    There's no trace of changing the values of row nor col in your loop.



  • Hi,

     int col = 0;
     int row = 0;
     int m_layout  = 4
     int lay_col= 1;
    
     for(int i = 0 ; i < m_layout ; i++ )
    {
        m_List[i].m_label = new QLabel;
        m_List[i].m_label ->setText("Label");
    
        m_List[i].widget = new QWidget;
        m_List[i].widget->setStyleSheet("background-color:blue");
      
        m_grid->addWidget(m_List[i].m_label,row,col);
        m_grid->addWidget(m_List[i].widget,row,col);
    
    
        col++;
    
    
        if(col == lay_col)
        {
            row++;
            col = 0;
        }
    }
    m_vbox->addLayout(m_grid);
    

    Thanks,


  • Lifetime Qt Champion

    You're adding both widgets to the same cell. If you want them both in the same cell then you have to put them in a layout and put that layout in the cell.



  • yup got it added both the widgets to vbox and then to gridlayout.

    Thanks,


  • Lifetime Qt Champion

    You're welcome !

    Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)


Log in to reply
 

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