Home > Net >  Multiple arguments in slot
Multiple arguments in slot

Time:06-02

Want to pass two arguments to the slot. I have ui->spinBox, it has two signals valueChanged(int) and valueChanged(QString). I'm doing like that:

connect(ui->spinBox,&QSpinBox::valueChanged, [&](){ this->ChangeParamCurve(ui->spinBox_11->value(),0);});

Appears error:

error: no matching member function for call to 'connect' qobject.h:463:41: note: candidate function not viable: no overload of 'valueChanged' matching 'const char *' for 2nd argument qobject.h:260:13: note: candidate template ignored: couldn't infer template argument 'Func1' qobject.h:300:13: note: candidate template ignored: couldn't infer template argument 'Func1' qobject.h:208:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided qobject.h:211:36: note: candidate function not viable: requires at least 4 arguments, but 3 were provided qobject.h:228:43: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided qobject.h:269:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided qobject.h:308:13: note: candidate function template not viable: requires at least 4 arguments, but 3 were provided

I think that qt can't accept overloaded signals or smth like that. Any thoughts?

CodePudding user response:

As shown in the docs that @chehrlic pointed out, you need to use QOverload when connecting to this signal.

connect(spinBox, QOverload<int>::of(&QSpinBox::valueChanged),
    [=](int i){ ChangeParamCurve(i, 0); });

Also, it doesn't matter how many parameters ChangeParamCurve takes. The lambda function is the slot that is being connected here. And it should take exactly one parameter, since that is what the signal is sending. But within the lambda, you can call functions with any number of parameters.

CodePudding user response:

I don't know how helpful it would be, but you can derive an object from the spinbox class yourself. and you can add signals and slots to it as you want.

  • Related