Qt5.6信号和插槽过载 [英] Qt5.6 signals and slots overloading

查看:127
本文介绍了Qt5.6信号和插槽过载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个用于处理从插槽接收的数据的类,并创建了多个具有不同参数类型的同名重载方法.

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)

发现这意味着它应该工作: http://doc.qt.io/qt-5/signalsandslots.html

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

但这不是...

来自"clsSlot"类:

From the class 'clsSlot':

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

实施:

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

接通电话:

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

pobjHandler是指向clsSlot实例的指针.

pobjHandler is an pointer to an instance of clsSlot.

推荐答案

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

上述代码的问题是notify函数不止一个(即,因为它已重载).您必须确切说明要连接的是哪一个.您可以使用强制转换,但是在这种情况下,您应该只使用较旧的Qt语法.例如:

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)));

很显然,上面的连接代码只是语法的一个示例,您需要用适当的代码(例如,用Fcs::Mount::GetRef()替换sender)来填充它.

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).

这是Qt文档的链接,解释了为什么必须使用旧语法用于超载.

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

超载

如您在上面的示例中可能看到的那样,由于error有重载,因此连接到QAbstractSocket :: error并不是很漂亮,并且获取重载函数的地址需要显式转换,例如以前建立的连接如下:

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));

不能简单地转换为:

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

...因为QSpinBox有两个带有不同参数的名为valueChanged()的信号.相反,新代码必须为:

...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);

某些宏可能会有所帮助(使用c11或typeof扩展名) 最好的办法可能是建议不要使信号或插槽过载...

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

…但是我们在Qt的先前次要版本中一直添加重载,因为获取函数的地址不是我们支持的用例.但是现在,在不破坏源代码兼容性的情况下,这是不可能的.

… 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.

这篇关于Qt5.6信号和插槽过载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆