信号发送者找不到正确的信号函数
connect(ui->LSpinBox,&QSpinBox::valueChanged,ui->hSlider,&QSlider::setValue);
QSpinBox的valueChanged函数分为int和QString两种,存在函数重载,需让编译器加以区分。
不区分的话会爆出:
error: C2664:
“QMetaObject::Connection QObject::connect(const QObject *,const char *,const char *,Qt::ConnectionType) const”:
无法将参数 2 从“overloaded-function”转换为“const char *”
Qt5官方文档推荐使用的方式
connect(comboBox, QOverload<int>::of(&QComboBox::activated),
[=](int index){ /* ... */ });
connect(comboBox, QOverload<const QString &>::of(&QComboBox::activated),
[=](const QString &text){ /* ... */ });
auto qOverload(T functionPointer)
Returns a pointer to an overloaded function. The template parameter is the list of the argument types of the function. functionPointer is the pointer to the (member) function:
struct Foo {
void overloadedFunction();
void overloadedFunction(int, const QString &);
};
... qOverload<>(&Foo::overloadedFunction)
... qOverload<int, const QString &>(&Foo::overloadedFunction)
If a member function is also const-overloaded qConstOverload and qNonConstOverload need to be used.
qOverload() requires C++14 enabled. In C++11-only code, the helper classes QOverload, QConstOverload, and QNonConstOverload can be used directly:
... QOverload<>::of(&Foo::overloadedFunction)
... QOverload<int, const QString &>::of(&Foo::overloadedFunction)
Note: Qt detects the necessary C++14 compiler support by way of the feature test recommendations from C++ Committee's Standing Document 6.
This function was introduced in Qt 5.7.
See also qConstOverload(), qNonConstOverload(), and Differences between String-Based and Functor-Based Connections.
创建一个函数指针用于保存指定的函数地址:
void (QComboBox:: * activatedInt)(int) = &QComboBox::activated;
void (QComboBox:: * activatedString)(QString) = &QComboBox::activated;
connect(comboBox, activatedInt,
[=](int index){ /* ... */ });
connect(comboBox, activatedString,
[=](const QString &text){ /* ... */ });