PyQt 中这两个 clicked() 信号有什么区别?

What is a difference between these two clicked() signals in PyQt?

QtCore.QObject.connect(my_button, QtCore.SIGNAL('clicked()'), my_func)

my_button.clicked.connect(my_func)

我通常使用第一个选项,但后来我找到了第二个,想知道它们之间有什么区别。

第一个选项是old-style signal and slot syntax, which is now obsolete. You can still use it in PyQt4, but it is not supported at all in PyQt5. The second option is the new-style signal and slot syntax,可以在PyQt5和所有最新版本的PyQt4中使用(它是在v4.5中引入的)。

PyQt 文档列出了旧式语法的以下缺点:

  • It requires knowledge of the C++ types of signal arguments.
  • It is error prone in that if you mis-type the signal name or signature - then no exception is raised, either when the signal is connected or emitted.
  • It is verbose.
  • It is not Pythonic.