vendredi 20 mars 2015

C++ Qt QTableWidget Checkbox get state and location

How to get state of all checkbox and get row and column of checked? Onclick PushButton function.



QTableWidget *t = ui->tableWidget;
t->setRowCount(2);
t->setColumnCount(2);

QStringList tableHeader;
tableHeader<<"item01"<<"item02";
t->setHorizontalHeaderLabels(tableHeader);

for (int i = 0; i < t->rowCount(); i++) {
for (int j = 0; j < t->columnCount(); j++) {
QWidget *pWidget = new QWidget();
QHBoxLayout *pLayout = new QHBoxLayout(pWidget);
QCheckBox *pCheckBox = new QCheckBox();
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0,0,0,0);
pLayout->addWidget(pCheckBox);
pWidget->setLayout(pLayout);
t->setCellWidget(i, j, pWidget);
}
}


And when I clicked the button, I need get all selected elements with rows, columns of each.



void Widget::on_pushButton_clicked()
{

// Code here

// For example: Selected ["item01", 2]
}

Aucun commentaire:

Enregistrer un commentaire