qt5 connect写法遇到重载的函数和信号槽
程序员文章站
2024-01-02 22:53:46
...
当connect想要连接一个重载函数时,因为名字是一样的,但是参数不同。用qt5的connect写法,编写时提示错误。
解决的办法是使用qOverload()或者把函数指针强制转换成对应的函数指针,需要Qt 5.7版本以上。
使用示例:
struct Foo {
void overloadedFunction();
void overloadedFunction(int, QString);
};
// requires C++14
qOverload<>(&Foo:overloadedFunction)
qOverload<int, QString>(&Foo:overloadedFunction)
// same, with C++11
QOverload<>::of(&Foo:overloadedFunction)
QOverload<int, QString>::of(&Foo:overloadedFunction)
I’m having trouble getting to grips with the new signal/slot syntax (using pointer to member function) in Qt 5,
as described in New Signal Slot Syntax. I tried changing this:
QObject::connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int));
to this:
QObject::connect(spinBox, &QSpinBox::valueChanged, slider, &QSlider::setValue);but I get an error when I try to compile it:
error: no matching function for call to QObject::connect(QSpinBox*&,, QSlider*&, void(QAbstractSlider:????)(int))
I’ve tried with clang and gcc on Linux, both with -std=c++11.
What am I doing wrong, and how can I fix it?