PDA

View Full Version : Is it possible to pass parameters to a slot that were not emitted by a signal?



kramed
15th August 2007, 19:51
Hi. I have numerous checkboxes that are each linked with a respective Spinbox. What I am trying to accomplish, with minimal code, is to append a suffix to the spinbox when the checkbox is checked. I have 8 sets of checkboxes/spinboxes and I am trying to make them utilize a single slot to accomplish my needs. I have tried the following by it seems a slot will not take a parameter unless it is emited by the accompanying signal. I was told briefly that QSignalMapper may be able to fit my needs but I am unsure. Any guidance would be appreciated! Thanks.

Connection:


QCheckBox* check_[8];
QDoubleSpinBox* grade_SpinBox_[8];


for (int i = 1; i <= 8; i++)
{
connect(checkbox_[i], SIGNAL(toggled(bool)), this, SLOT(append_suffix(bool, [i])));
}


Slot:


void MainWindow::append_suffix(bool clicked, int checkboxnum)
{
if (clicked == false)
{
grade_SpinBox_[checkboxnum]->setSuffix(QApplication::translate("MainWindow", "%", 0, QApplication::UnicodeUTF8));
}
else
{
ass_grade_SpinBox_[checkboxnum]->setSuffix(QApplication::translate("MainWindow", "", 0, QApplication::UnicodeUTF8));
}
}

jpn
15th August 2007, 21:45
Yes, you could use QSignalMapper like this:


// QCheckBox* check_[8];

{
QSignalMapper* mapper = new QSignalMapper(this);
for (int i = 0; i < 8; ++i)
{
mapper->setMapping(check_[i], i);
connect(check_[i], SIGNAL(toggled(bool)), mapper, SLOT(map()));
}
connect(mapper, SIGNAL(mapped(int)), this, SLOT(append_suffix(int)));
}

void append_suffix(int i)
{
if (check_[i]->isChecked())
{
// ...
}
else
{
// ...
}
}

kramed
16th August 2007, 00:20
Man, jpn, you are great. Two days in a row you have helped me greatly. TT should hire you to help with the documentation, your examples are much easier to follow. Thank you!