Why is PyQt connect() syntax so verbose?

2019-08-27 14:02发布

I'm just learning PyQt and looking at the Signals and Slots mechanism. I'm a bit baffled by the verbose syntax. Why do we have:

self.connect(dial, SIGNAL("valueChanged(int)"), spinbox.setValue)

I would much prefer to write the following:

self.connect(dial.valueChanged, spinbox.setValue)

Can anyone tell me why the connect() syntax needs to be so explicit/verbose?

3条回答
Anthone
2楼-- · 2019-08-27 14:38

Luper's answer is much better than this one, but for the sake of completeness...

The ugly "old style" syntax is an anachronism from the C++ world - just look at the syntax those guys have to work with! Yucky...

查看更多
Evening l夕情丶
3楼-- · 2019-08-27 14:50

An even shorter way is to assign the signal name to the function in the keyword arguments of the constructor e.g. QDial(valueChanged=spinbox.setValue). PyQt will automatically connect the valueChanged() signal to spinbox.setValue().

查看更多
一夜七次
4楼-- · 2019-08-27 14:54

You can use PyQt's new style signals which are less verbose:

self.connect(dial, SIGNAL("valueChanged(int)"), spinbox.setValue)

Becomes:

dial.valueChanged.connect(spinbox.setValue)
查看更多
登录 后发表回答