Qt5.6 signals and slots overloading

六眼飞鱼酱① 提交于 2019-12-11 13:53:38

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/37463291/qt5-6-signals-and-slots-overloading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!