[Development] [RFC] What to do about overloaded signals/slots?

Olivier Goffart olivier at woboq.com
Sat Oct 5 18:24:56 CEST 2013


Hi,

As you may or may not know, it is a bit tricky to use the pointer based 
connection syntax with overloaded signals or slot.

If you do, say:
 
 connect(process, &QProcess::error;
     this , &MyObject::processError);

You get a compilation error looking like this

 error:   no matching function for call to 'QObject::connect(QProcess*,
        <unresolved overloaded function type>, ...

Because QProcess::error is overloaded and the compiler can detect its type.

They way around is to manually specify the type.  This is a bit difficult, and 
many developers get problem with it. Which is why I made a patch to qdoc to 
always show an example.

  https://codereview.qt-project.org/67348

This will add, for each overloaded signal a note like:

"
Note:This signal is overloaded with another member this class. In order to 
connect to this signal using the function pointer syntax, you need to help the 
compiler by specifying the type of the signal. Example:

 connect(process, static_cast<void(QProcess::*)
(QProcess::ProcessError)>(&QProcess::error),
   [=](QProcess::ProcessError error){ /* ... */ });

"

This is only for signal and not for slots.  And I used in the example a 
static_cast to specify the type. The adventage with this approach is that it 
works both for the signal and the slot.  But there are other ways. 

Specifying the template parameter:

 connect<void(QProcess::*)(QProcess::ProcessError)>(process, &QProcess::error,
   [=](QProcess::ProcessError error){ /* ... */ });

This is actually shorter to type.  But for slot you would need to speficy both 
the type of the signal and of the slot.

Which one should we recommand?  Should we come to a macro?
Should the documentation also show a message for the slots that have 
overloads?


I would also like to remind that adding overloads to functions is not source 
compatible,  as it may break a connection using the function poitner syntax.
Overloaded signals and slots should be avoided.


And another note: I see that Qt's source code does not make much use yet of 
the new syntax.  I would like to recommand using it as it allow to catch more 
errors at compile time.

-- 
Olivier

Woboq - Qt services and support - http://woboq.com - http://code.woboq.org




More information about the Development mailing list