Usage of QSignalMapper with checkboxes in QTableWidget
Hey there,
I'm new here, but not an absolut newbe with qt.
I have to ask here, because i didn't find any solution for my problem.
On My window I have a QTableWidget, wich I'm going to fill with checkboxes at runtime. I need signals for these checkboxes which contain the row count and the state from the checkbox signal toggled(bool state).
I tried the following, but it doesn't work. I actual don't understand the whole thing with QSignalMapper.
In the constructor I did:
My first try of the signal mapper:
Code:
ui->table->setRowCount(ui->table->rowCount()+1);
int index = ui->table->rowCount()-1;
new_checkbox->setChecked(true);
connect(new_checkbox,SIGNAL(toggled(bool)),checkboxMapper,SLOT(map(bool)));
checkboxMapper->setMapping(new_checkbox,(index,new_checkbox->isChecked()));
connect(checkboxMapper,SIGNAL(mapped(int,bool)),this,SLOT(checkbox_toggled(int,bool)));
ui->table->setCellWidget(index,1,new_checkbox);
I hope my english is not too confusing and you could help me.
Kind regards
Re: Usage of QSignalMapper with checkboxes in QTableWidget
The first connect fails because there is no slot QSignalMapper::map(bool).
I wonder why the setMapping call even compiles, I guess you wanted to call setMapping(QObject*, int)
The second connect fails because there is no signal QSIgnalMapper::mapped(int, bool);
The signal mapper will emit a signal with and argument value that was the second argument to the setMapping call for the respective sender object.
E.g. if you call
Code:
mapper->setMapping(sender, 1);
then the connected signal of sender will lead to
being emitted.
In your case the row. Which you can then use to retrieve the checkbox.
Cheers,
_
Re: Usage of QSignalMapper with checkboxes in QTableWidget
Thanks a lot. I wondered if there is an easier solution for this..
Now the code looks like this and it works!
Code:
ui->table->setRowCount(ui->table->rowCount()+1);
int index = ui->table->rowCount()-1;
new_checkbox->setChecked(true);
connect(new_checkbox,SIGNAL(clicked()),checkboxMapper,SLOT(map()));
checkboxMapper->setMapping(new_checkbox,index);
connect(checkboxMapper,SIGNAL(mapped(int)),this,SLOT(checkbox_toggled(int)));
ui->table->setCellWidget(index,1,new_checkbox);
with the slot:
Code:
void window::checkbox_toggled(int index)
{
bool state = static_cast<QCheckBox*>(ui->table->cellWidget(index,1))->isChecked();
main_window->do something(index,state);
}
That produces the behavior I wanted :)