Qt5.6信号与槽重载
Qt5.6 signals and slots overloading
我创建了一个 class 来处理从插槽接收的数据,并创建了几个具有不同参数类型的同名重载方法。
是否可以将重载方法用作插槽?
到目前为止我有两个声明:
void notify(uint uintData);
void notify(float fltData);
但是第二个在运行时产生警告:
QObject::connect: No such slot clsSlot::notify(float)
发现这意味着它应该有效:http://doc.qt.io/qt-5/signalsandslots.html
但事实并非如此...
来自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 实例的指针。
QObject::connect(Fcs::Mount::GetRef()
,&Fcs::Mount::signalElevation
,pobjHandler, &clsSlot::notify);
上述代码的问题在于 notify
函数不止一个(即,因为它已过载)。您必须准确说明要连接的是哪一个。你可以用强制转换来完成,但在这种情况下你应该只使用旧的 Qt 语法。例如:
connect(sender, SIGNAL(signalFn(float)), this, SLOT(notify(float)));
connect(sender, SIGNAL(signalFn(uint)), this, SLOT(notify(uint)));
显然,上面的连接代码只是一个语法示例,您需要用适当的代码填充它(例如,也许将 sender
替换为 Fcs::Mount::GetRef()
)。
编辑:这是 Qt 文档的 link,解释了为什么旧语法必须用于重载。
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.
我创建了一个 class 来处理从插槽接收的数据,并创建了几个具有不同参数类型的同名重载方法。
是否可以将重载方法用作插槽?
到目前为止我有两个声明:
void notify(uint uintData);
void notify(float fltData);
但是第二个在运行时产生警告:
QObject::connect: No such slot clsSlot::notify(float)
发现这意味着它应该有效:http://doc.qt.io/qt-5/signalsandslots.html
但事实并非如此...
来自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 实例的指针。
QObject::connect(Fcs::Mount::GetRef()
,&Fcs::Mount::signalElevation
,pobjHandler, &clsSlot::notify);
上述代码的问题在于 notify
函数不止一个(即,因为它已过载)。您必须准确说明要连接的是哪一个。你可以用强制转换来完成,但在这种情况下你应该只使用旧的 Qt 语法。例如:
connect(sender, SIGNAL(signalFn(float)), this, SLOT(notify(float)));
connect(sender, SIGNAL(signalFn(uint)), this, SLOT(notify(uint)));
显然,上面的连接代码只是一个语法示例,您需要用适当的代码填充它(例如,也许将 sender
替换为 Fcs::Mount::GetRef()
)。
编辑:这是 Qt 文档的 link,解释了为什么旧语法必须用于重载。
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.