Multiple arguments in slot

290 Views Asked by At

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?

2

There are 2 best solutions below

0
JarMan On BEST ANSWER

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.

0
poison pawn On

You can derive an object from the spinbox class yourself. You would be able to add signals and slots to it as you want.

I once did something like this because there was no "focus" event for qlineedit. Let me throw in the main idea of what I wrote for qlineedit.

If you want to have a focused signal, you will have to derive the QLineEdit class. Here is a sample of how this can be achieved.

In the myLineEdit.h file you have:

class MyLineEdit : public QLineEdit
{
  Q_OBJECT

public:
  MyLineEdit(QWidget *parent = 0);
  ~MyLineEdit();

signals:
  void focussed(bool hasFocus);

protected:
  virtual void focusInEvent(QFocusEvent *e);
  virtual void focusOutEvent(QFocusEvent *e);
};

In the myLineEdit.cpp file you have:

MyLineEdit::MyLineEdit(QWidget *parent)
 : QLineEdit(parent)
{}

MyLineEdit::~MyLineEdit()
{}

void MyLineEdit::focusInEvent(QFocusEvent *e)
{
  QLineEdit::focusInEvent(e);
  emit(focussed(true));
}

void MyLineEdit::focusOutEvent(QFocusEvent *e)
{
  QLineEdit::focusOutEvent(e);
  emit(focussed(false));
}

You can now connect the MyLineEdit::focussed() signal to your focus() method (slot).

In the same way two events can trigger your custom event with custom parameters. Again, if you wish, you can add these features to an existing spinbox, from the Designer, by right-clicking and using the Promote to method.