5

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();
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
JokerMartini
  • 5,674
  • 9
  • 83
  • 193
  • The signal is `SIGNAL(valueChanged(double))`. – Benjamin Bihler Sep 02 '19 at 14:54
  • Im trying to call the method slotInputChanged from two different spinbox's one is an Int spinner and the other is a double. – JokerMartini Sep 02 '19 at 15:00
  • 1
    @JokerMartini use `connect(uiSpinBox, SIGNAL(valueChanged(int)), this, SLOT(slotInputChanged)); connect(uiDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(slotInputChanged));` – eyllanesc Sep 02 '19 at 15:01
  • why do people make comments with answers....please respond with an actual Answer to the question so i can mark it – JokerMartini Sep 02 '19 at 15:04
  • Rather a hint than an answer: The style of connects is the old one which resolves connection at runtime. You may investigate into the [New Signal Slot Syntax](https://wiki.qt.io/New_Signal_Slot_Syntax) which came with Qt5. It allows to detect broken connections at compile time. – Scheff's Cat Sep 02 '19 at 15:11
  • @Scheff could you demonstrate how to make the connection with the new syntax please? – JokerMartini Sep 02 '19 at 15:12
  • @JokerMartini I'm too slow - it's already done. ;-) – Scheff's Cat Sep 02 '19 at 15:14

2 Answers2

10

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);
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • @CharonX It was obvious, in Qt5 the new connection syntax should be preferred. – eyllanesc Sep 02 '19 at 15:14
  • @Scheff in reality the overload could cause an error in the future but in this case what generates the error is that the signature is not used. – eyllanesc Sep 02 '19 at 15:16
  • @Scheff I use both syntax depending on whether it is Qt4 or Qt5 since although Qt5 has been going on for more than 5 years there are still projects maintained with Qt4. – eyllanesc Sep 02 '19 at 15:18
  • 2
    @eyllanesc yup. I kinda use the new style exclusively, much better to have the compiler complain than to have to watch the outstream if there is some qWarning along the lines of "Connection to Bla did not work because there was matching SIGNAL/SLOT" :) – CharonX Sep 02 '19 at 15:24
4

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

CharonX
  • 2,130
  • 11
  • 33