piepolitb
piepolitb

Reputation: 31

qt signal-slot on QCheckBox(es) previously added recursively

I'm adding some checkBoxes recursively in tableWidget cells (always in column 1) on a specific function. The final nummber of those checkboxes is not defined and depends on the user input.

QStringList matID;
for(int i=0; i<matID.size(); i++
{
   ui->tableWidget->insertRow( ui->tableWidget->rowCount() );

   QCheckBox *checkBox = new QCheckBox();
   ui->tableWidget->setItem(ui->tableWidget->rowCount()-1,0,checkBox)
}

I would like to connect each one of them with a signal-slot connection which tells me which one has been checked.

Can anyone help me? I don't really understand how to do it...

thanks Giuseppe

Upvotes: 0

Views: 647

Answers (1)

Dani&#235;l Sonck
Dani&#235;l Sonck

Reputation: 889

There are four solutions for this problem. I'll only describe the cleanest possiblity.

The best solution is to actually to make your item checkable. You can do this by setting the Qt::ItemIsUserCheckable flag on the item using setFlags(), remember to also keep the defaults.

QStringList matID;
for(int i=0; i<matID.size(); i++
{
   ui->tableWidget->insertRow( ui->tableWidget->rowCount() );

   QTableWidgetItem *item = ui->tableWidget->getItem(ui->tableWidget->rowCount()-1,0);
   item->setFlags(item->getFlags() | Qt::ItemIsUserCheckable);
}

Then, as explained in Qt/C++: Signal for when a QListWidgetItem is checked? (ItemWidgets are similar enough), you can then listen on itemChanged and check using checkState().

The other methods would be:

  • set an delegate for your column

  • create a QSignalMapper to map your own QCheckBox pointers to some value identifying your rows

  • Use the QObject::sender() member function to figure out which checkbox was responsible, and resolve it back to the row on your own.

Upvotes: 1

Related Questions