无法 QOverload 专用信号,使用 Qt 文档示例

Can't QOverload private signal, using Qt docs example

Here in the Qt documentation写成:

Note: This is a private signal. It can be used in signal connections but cannot be emitted by the user.

Note: Signal activated is overloaded in this class. To connect to this signal by using the function pointer syntax, Qt provides a convenient helper for obtaining the function pointer as shown in this example:

connect(socketNotifier, QOverload<QSocketDescriptor, QSocketNotifier::Type>::of(&QSocketNotifier::activated),
    [=](QSocketDescriptor socket, QSocketNotifier::Type type){ /* ... */ });

当我使用它时,我遇到了这个问题:

error: no matching function for call to 'of'

/usr/include/qt5/QtCore/qglobal.h:1224: candidate template ignored: could not match 'type-parameter-0-0 (QSocketDescriptor, QSocketNotifier::Type) const' against 'void (QSocketDescriptor, QSocketNotifier::Type, QSocketNotifier::QPrivateSignal)'

/usr/include/qt5/QtCore/qglobal.h:1212: candidate template ignored: failed template argument deduction

/usr/include/qt5/QtCore/qglobal.h:1241: candidate template ignored: could not match 'R (*)(QSocketDescriptor, QSocketNotifier::Type)' against 'void (QSocketNotifier::*)(QSocketDescriptor, QSocketNotifier::Type, QSocketNotifier::QPrivateSignal)'

我只是 copy-pasted 这个例子,<QSocketDescriptor><QSocketNotifier> headers 都包括在内。我错过了什么?

如果可以使用 C++14,则可以使用辅助函数:

template<class T> auto privateOverload(void ( QSocketNotifier::* s)( QSocketDescriptor,QSocketNotifier::Type,T ) ){return s;}

然后你就可以使用

QObject::connect(socketNotifier, privateOverload(&QSocketNotifier::activated),/*...*/);

如果你可以使用 C++20,你也可以直接在连接语句中使用 lambda:

QObject::connect(socketNotifier, []<class T>(void ( QSocketNotifier::* s)( QSocketDescriptor,QSocketNotifier::Type,T ) ){return s;}(&QSocketNotifier::activated),/*...*/);