1

I have created a class for handling data received from slots and have created several overloaded methods of the same name with a different parameter type.

Is it possible to use overloaded methods as slots?

I have two declarations so far:

    void notify(uint uintData);
    void notify(float fltData);

However the 2nd produces a warning at runtime:

    QObject::connect: No such slot clsSlot::notify(float)

Found this which implies it should work: http://doc.qt.io/qt-5/signalsandslots.html

But it doesn't...

From the class 'clsSlot':

    public slots:
        void notify(uint uintValue);
        void notify(float fltValue);

Implementation:

    void clsSlot::notify(float fltValue) {
        notifyPrimitive(meID, QString::number(fltValue));
    }
    void clsSlot::notify(uint uintValue) {
        notifyPrimitive(meID, QString::number(uinValue));
    }

Connect call:

        QObject::connect(Fcs::Mount::GetRef()
                        ,&Fcs::Mount::signalElevation
                        ,pobjHandler, &clsSlot::notify);

pobjHandler is an pointer to an instance of clsSlot.

SPlatten
  • 5,334
  • 11
  • 57
  • 128

1 Answers1

3
QObject::connect(Fcs::Mount::GetRef()
    ,&Fcs::Mount::signalElevation
    ,pobjHandler, &clsSlot::notify);

The problem with your above code is that there is more than one notify function (i.e., since it's overloaded). You have to state exactly which one is to be connected. You could do it with a cast but in this case you should just use the older Qt syntax. For example:

connect(sender, SIGNAL(signalFn(float)), this, SLOT(notify(float)));
connect(sender, SIGNAL(signalFn(uint)), this, SLOT(notify(uint)));

Obviously, the above connect code is just an example of the syntax and you'll need to fill it in with your appropriate code (e.g., replace sender with Fcs::Mount::GetRef() perhaps).


Edit: Here is a link to the Qt documentation explaining why the old syntax must be used for overloads.

Overload

As you might see in the example above, connecting to QAbstractSocket::error is not really beautiful since error has an overload, and taking the address of an overloaded function requires explicit casting, e.g. a connection that previously was made as follows:

connect(mySpinBox, SIGNAL(valueChanged(int)), mySlider, SLOT(setValue(int));

cannot be simply converted to:

connect(mySpinBox, &QSpinBox::valueChanged, mySlider, &QSlider::setValue);

...because QSpinBox has two signals named valueChanged() with different arguments. Instead, the new code needs to be:

connect(mySpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), mySlider, &QSlider::setValue);

Some macro could help (with c11 or typeof extensions) The best thing is probably to recommend not to overload signals or slots …

… but we have been adding overloads in past minor releases of Qt because taking the address of a function was not a use case we support. But now this would be impossible without breaking the source compatibility.

James Adkison
  • 9,412
  • 2
  • 29
  • 43
  • I was hoping that the compiler would select the correct instance of notify for me, as thats why I overloaded it. – SPlatten May 26 '16 at 14:38
  • @SPlatten The correct overload will be called but you must explicitly connect each overload. – James Adkison May 26 '16 at 14:39
  • Trying it, but not getting any further, it compiles but signals don't get to slots. – SPlatten May 26 '16 at 14:40
  • @SPlatten You must have a problem somewhere else. I added a link to the Qt documentation explaining the new and old syntax. It explicitly talks about overloaded slots. – James Adkison May 26 '16 at 14:52