Why is PyQt connect() syntax so verbose?

南笙酒味 提交于 2020-01-06 06:40:30

问题


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?


回答1:


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)



回答2:


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...




回答3:


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().



来源:https://stackoverflow.com/questions/50184839/typeerror-native-qt-signal-is-not-callable-when-i-try-to-convert-pyqt4-to-pyqt5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!