Passing Parameters to Slots
-
A slot seems to work as expected without a parameter passed to it as follows:
@ connect (ui->pushButton, SIGNAL (clicked()), this, SLOT(goal_seek()));
//...code...
void MainWindow::goal_seek()
{
double seek = 5;
QTableWidgetItem *itab7= new QTableWidgetItem;
QString A2 = QString::number(seek);
itab7->setText(A2);
ui->tableWidget_2->setItem(0,7, itab7);
}
@However, when I use the same code, the slot does not work with the parameter passed to it, as shown below:
@
connect (ui->pushButton, SIGNAL (clicked()), this, SLOT(goal_seek(5.0)));void MainWindow::goal_seek(double seek)
{
double seek = 5;
QTableWidgetItem *itab7= new QTableWidgetItem;
QString A2 = QString::number(seek);
itab7->setText(A2);
ui->tableWidget_2->setItem(0,7, itab7);
}
@Can one use parameters for slots, and if so, how?
-
You are doing it wrong. You can't pass static values to slots in the connect assignment. You need to emit a value together with your signal:
@
// header
signals:
void clicked(int) const;// source
connect (usomeObject, SIGNAL (clicked(int)), this, SLOT(goal_seek(int)));// signal emission:
emit clicked(5);
@ -
It doesn't work. Where does the emit code go?
-
One of solutions is:
@ connect (ui->pushButton, SIGNAL (clicked()), this, SLOT(some_slot()));
//code...
void MainWindow::some_slot()
{
goal_seek(5);
}
@
or
@
connect (ui->pushButton, SIGNAL (clicked()), this, SLOT(some_slot()));
connect (ui->pushButton, SIGNAL (clicked(int)), this, SLOT(goal_seek(int)));
//code...void MainWindow::some_slot()
{
emit clicked(5);
}
@
it depends from your code structure.