[Solved] QGridLayout with two columns



  • Hi,
    I have a QStringList and going to create radio buttons and need to put them in a goupbox.

    My String list as below.

    @QStringList Dialog::existingSizes()
    {
    QStringList sizes;
    sizes << "400X1400" << "400X1600" << "400X1800" << "400X2000"<< "600X1400" << "600X1600" << "600X1800" << "600X2000";
    return sizes;
    }@

    And radiobuttons creates as below

    @foreach(size, existingSizes())
    {
    rb = new QRadioButton(size);
    grid->addWidget(rb);
    }
    ui->groupBox->setLayout(grid);@

    I know there is a way to handle the position using column no and raw no.

    But how I get the layout as below.

    @400X1400 600X1400
    400X1600 600X1600
    400X1800 600X1800
    400X2000 600X2000@

    Thanking you.


  • Moderators

    something like this?

    filling row-wise from left to right:
    @
    ....

    int colCount = 2;
    QStringList sizeList = existingSizes();
    for(int i = 0; i < sizeList.count(); ++i)
    {
    int row = i / colCount;
    int col = i % colCount;

    QString size = sizeList.value(i);
    rb = new QRadioButton(size);
    grid->addWidget(rb, row, col);
    

    }
    @

    filling col-wise from top to bottom:
    @
    ....

    int rowCount = 4;
    QStringList sizeList = existingSizes();
    for(int i = 0; i < sizeList.count(); ++i)
    {
    int row = i % rowCount;
    int col = i / rowCount;

    QString size = sizeList.value(i);
    rb = new QRadioButton(size);
    grid->addWidget(rb, row, col);
    

    }
    @



  • @raven-worx,

    Thanks a lot. It work as magic. :)


Log in to reply
 

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