PDA

View Full Version : Usage of QSignalMapper with checkboxes in QTableWidget



quizzmaster
14th September 2014, 00:43
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:

checkboxMapper = new QSignalMapper(this);

My first try of the signal mapper:

ui->table->setRowCount(ui->table->rowCount()+1);

int index = ui->table->rowCount()-1;

QCheckBox *new_checkbox = new QCheckBox(this);
new_checkbox->setChecked(true);
connect(new_checkbox,SIGNAL(toggled(bool)),checkbo xMapper,SLOT(map(bool)));
checkboxMapper->setMapping(new_checkbox,(index,new_checkbox->isChecked()));
connect(checkboxMapper,SIGNAL(mapped(int,bool)),th is,SLOT(checkbox_toggled(int,bool)));

ui->table->setItem(index,0,new QTableWidgetItem("Some Stuff"));
ui->table->setCellWidget(index,1,new_checkbox);

I hope my english is not too confusing and you could help me.

Kind regards

anda_skoa
14th September 2014, 08:38
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


mapper->setMapping(sender, 1);

then the connected signal of sender will lead to


mapped(1);

being emitted.

In your case the row. Which you can then use to retrieve the checkbox.

Cheers,
_

quizzmaster
14th September 2014, 11:13
Thanks a lot. I wondered if there is an easier solution for this..

Now the code looks like this and it works!


ui->table->setRowCount(ui->table->rowCount()+1);

int index = ui->table->rowCount()-1;

QCheckBox *new_checkbox = new QCheckBox(this);
new_checkbox->setChecked(true);
connect(new_checkbox,SIGNAL(clicked()),checkboxMap per,SLOT(map()));
checkboxMapper->setMapping(new_checkbox,index);
connect(checkboxMapper,SIGNAL(mapped(int)),this,SL OT(checkbox_toggled(int)));

ui->table->setItem(index,0,new QTableWidgetItem(QString::fromStdString(molecule->formula())));
ui->table->setCellWidget(index,1,new_checkbox);

with the slot:

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 :)