Home > database >  Can't QOverload private signal, using Qt docs example
Can't QOverload private signal, using Qt docs example

Time:10-09

Here in the Qt documentation is written:

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){ /* ... */ });

When I use it, I'm getting this issue:

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)'

I just copy-pasted the example and have <QSocketDescriptor> and <QSocketNotifier> headers both included. What am I missing?

CodePudding user response:

If you can use C 14 you can use a helper function:

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

and then you can use

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

If you can use C 20 you can also use a lambda directly in the connect statement:

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