编译器选择了错误的重载函数

Compiler picks wrong overloaded function

我即将调整我的源代码以适应 Qt 中新的信号和槽语法。虽然下面陈述的代码在已弃用的 const char* signal 参数下运行良好,但它不适用于新的 QMetaMethod &signal 语法。

class SignalWaiter : public QObject {
    Q_OBJECT
public:
    SignalWaiter(const QObject* sender, const QMetaMethod &signal);

private slots:
    void signalCaught();
};

SignalWaiter::SignalWaiter(const QObject* sender, const QMetaMethod &signal) {
    QObject::connect(sender, signal, this, &SignalWaiter::signalCaught);
}

void SignalWaiter::signalCaught() {
}

编译器在 connect() 命令处停止并显示消息:

error: C2664: 'QMetaObject::Connection QObject::connect(const QObject *,const char *,const char *,Qt::ConnectionType) const': cannot convert argument 2 from 'const QMetaMethod' to 'const char *'

No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

很明显,编译器试图用旧语法调用重载的 connect 方法。我做错了什么?

connect() you're trying to use 有签名:

QMetaObject::Connection QObject::connect(
    const QObject *sender, 
    const QMetaMethod &signal,
    const QObject *receiver,
    const QMetaMethod &method,
    Qt::ConnectionType type = Qt::AutoConnection)

注意第 4th 参数 const QMetaMethod &method 不是指向成员的指针,这就是为什么会出错。

要进行适当的转换,您可以使用:

auto metaSlot = metaObject()->method(metaObject()->indexOfSlot("signalCaught()"));
QObject::connect(sender, signal, this, metaSlot);

不过,正如@p-a-o-l-o 所指出的,新的 signal/slot 语法使用指向成员函数的指针,而不是 QMetaMethod。他的解决方案可能正是您真正想要的。

我认为 O'Neil 的回答解决了编译问题,但如果 OP 真的想要

adapt my sources to the new signals and slots syntax

也许他们想给 SignalWaiter class 另一个构造函数,像这样:

template<typename T>
SignalWaiter(const T* sender, void (T::* signal)())
{
    QObject::connect(sender, signal, this, &SignalWaiter::signalCaught);
}

有 class MyClassvoid mysignal() 信号:

MyClass * myclass = new MyClass();
SignalWaiter * waiter = new SignalWaiter(myclass, &MyClass::mysignal);

改变

QObject::connect(sender, signal, this, &SignalWaiter::signalCaught);

QObject::connect(sender, signal, this, QMetaMethod::fromSignal(&SignalWaiter::signalCaught));

对我有用,这类似于 O'Neil 的回答,但在语法方面更短一些。请注意,您还需要在 QMetaMethod::fromSignal() 命令中的每个构造函数调用中包装信号参数。

p-a-o-l-o提出的解决方案对我没有用,因为我的SignalWaiterclass实际上继承自QObject,它禁止使用模板。