[Interest] Problem about signals/slots in Qt 5

Konrad Rosenbaum konrad at silmor.de
Tue Sep 18 17:00:44 CEST 2012

On Tuesday 18 September 2012 14:21:58 Stephen Chu wrote:
> On 9/18/12 4:10 AM, Thiago Macieira wrote:
> >> How could I solve this?
> >> 
> >> In Qt 4 I could specialize the signal using SIGNAL(valueChanged(int)),
> >> how could I do in Qt 5?
> >> 
> >      void (QSpinBox:: *signal)(int) = &QSpinBox::valueChanged;
> >      QObject::connect(spinBox, signal, slider, &QSlider::setValue);
> or:
> QObject::connect(spinBox, (void (QSpinBox:: *)(int))
> &QSpinBox::valueChanged, slider, &QSlider::setValue);
> if you want to keep it in one line.
> I like this new syntax much better since it finds signal/slot mismatches
> at compile time. I used to miss the connection mismatch messages in the
> sea of my REALLY chatty logs. And pulling my hairs wondering why
> something doesn't work. :)

Actually using C-style casts you still run the risk of creating garbage if you 
change the parameter types. Above: if QSpinBox had a signal 
valueChanged(float) and no other override for that method name it would go 
undetected and may lead to interesting situations during runtime.


QObject::connect(spinBox, static_cast<void (QSpinBox:: *)(int)> 
(&QSpinBox::valueChanged), slider, &QSlider::setValue);

This way whenever you change the parameter type(s) the compiler will detect 

I'm not sure how to detect a misalignment between instance type and the class 
name in the cast. E.g. if spinBox was not a QSpinBox, but a QLineEdit.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120918/afc3bc4d/attachment.sig>

More information about the Interest mailing list