Getting state of QCheckBox in a QGridLayout?

1.8k views Asked by At

I have a simple layout to get/set states of IOs of an USB board. Each row corresponds to a module and column to an IO pin. THe purpose is to set states of outputs on one row, and see if the inputs match the ones checked on another row.

I made this to setup the layout

QLabel * templab = new QLabel;
templab ->setText(QString("Broche ->"));
gridLayout_2->addWidget(templab, 0, 0);
for (int row = 1; row < modules+1 ;row++)
{
    QLabel * templab = new QLabel;
    templab ->setText(QString("Module %1").arg(row-1));
    gridLayout_2->addWidget(templab, row, 0);
}
for (int col = 1; col < colonnes+1 ;col++)
{
    QLabel * templab = new QLabel;
    templab ->setNum(col);
    gridLayout_2->addWidget(templab, 0, col);
}
for (int row = 1; row < modules +1 ;row++)
{
    for (int col = 1; col < colonnes+1 ;col++)
    {
        QCheckBox* checkBox = new QCheckBox();
        gridLayout_2 ->addWidget(checkBox,row,col);
    }
}

How do I check the state of checkboxes ?

I did not find any clue on how to get the state of a checkbox using gridLayout_2->itematposition(x,y)

Thanks a lot.

Edit: following Spyke advice, I used :

QCheckBox * checkBox = findChild<QCheckBox*>(ui->gridLayout_5->itemAtPosition(x,y)->widget()->objectName());

There's another way to do it that I discovered:

QCheckBox * ios[8][16];

 for (int row = 1; row < modules +1 ;row++)
    {
        for (int col = 1; col < colonnes+1 ;col++)
        {
            ios[row-1][col-1]= new QCheckBox();
            ui->gridLayout_5 ->addWidget(ios[row-1][col-1],row,col);
        }
    }

and to test the state:

if (ios[x][y]->checkState() == Qt::Checked)
{
    qDebug()<<"Checked"<<endl;
}
else
    qDebug()<<"UN Checked"<<endl;

}
1

There are 1 answers

1
ScarCode On BEST ANSWER

You can use gridLayout->findChild<QCheckBox*>(gridLayout->itemAt(0)->widget()->objectName());