QSpinBox and QDoubleSpinBox do not call method on valueChanged

6.7k Views Asked by At

All i want to do is call a method when the value of a qspinbox and a doublespinbox are changed.

I do not need the actual value from the spinbox being changed, i just want it to trigger the calling of another method. Why does the code below not error or do anything at all? Not even call the method?

cpp

connect(uiSpinBox, SIGNAL(valueChanged()), this, SLOT(slotInputChanged));
connect(uiDoubleSpinBox, SIGNAL(valueChanged()), this, SLOT(slotInputChanged));

void ColorSwatchEdit::slotInputChanged()
{
    qDebug() << "Im here";
}

header

public:
    QSpinBox *uiSpinBox;
    QDoubleSpinBox *uiDoubleSpinBox;

public slots:
    void slotInputChanged();
2

There are 2 best solutions below

4
On BEST ANSWER

Even if you do not use the data that carries the signal you must establish the signature in the connection:

connect(uiSpinBox, SIGNAL(valueChanged(int)), this, SLOT(slotInputChanged)); 
connect(uiDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(slotInputChanged));

But it is recommended that you use the new connection syntax as it would have indicated the error:

connect(uiSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &ColorSwatchEdit::slotInputChanged); 
connect(uiDoubleSpinBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &ColorSwatchEdit::slotInputChanged);
0
On

In addition to eyllanesc's answer, consider using the FunctionPointer syntax if possible, i.e.

connect(uiSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &YourClass::slotInputChanged)

and

connect(uiDoubleSpinBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &YourClass::slotInputChanged)

this way the compiler can tell at compile time you if the connection cannot be resolved