Arranging the grid layout in function



  • Hi,

    I have a grid layout of 2x2
    aligned to QVBoxLayout.

    This code is in constructor.

    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);
    setLayout(m_vbox)
    

    From the function when i need to change the gridlayout to the corresponding values provided from the function.
    for example 2x3 or so .

    How can i do it, guidance is required.

    Thanks,


  • Qt Champions 2016

    Something along those lines:

    qint32 columns = 5; // Or whatever comes as an argument
    QList<QWidget *> itemsToInsert;
    QGridLayout * layout;
    
    for (qint32 i = 0, size = itemsToInsert.size(); i < size; i++)
        layout->addWidget(itemsToInsert[i], i / columns, i % columns);
    

Log in to reply
 

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