我的应用程序仅通过右键单击托盘图标并按“退出”:
退出class DialogUIAg(QDialog):
...
self.quitAction = QAction("&Quit", self, triggered=qApp.quit)
以下模块是应用程序的起点:
#!/usr/bin/env python
import imgAg_rc
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import appLogger
from runUIAg import *
class Klose:
""" Not sure if i need a Class for it to work"""
def closingStuff(self):
print("bye")
@pyqtSlot()
def noClassMethod():
print("bye-bye")
app = QApplication(sys.argv)
QApplication.setQuitOnLastWindowClosed(False)
k = Klose()
app.connect(app, SIGNAL("aboutToQuit()"), k,SLOT("closingStuff()")) #ERROR
app.connect(app, SIGNAL("aboutToQuit()"), k.closingStuff) # Old-Style
app.connect(app, SIGNAL("aboutToQuit()"), noClassMethod) # Old-Style
app.aboutToQuit.connect(k.closingStuff) # New-Style
app.aboutToQuit.connect(noClassMethod) # New-Style
winUIAg = DialogUIAg()
winUIAg.show()
app.exec_()
我的意图是当应用程序是关于ToQuit时执行一段代码 这是我得到的错误:
$ ./rsAg.py
Traceback (most recent call last):
File "./rsAgent.py", line 20, in <module>
app.connect(app, SIGNAL("aboutToQuit()"), k,SLOT("closingStuff()"))
TypeError: arguments did not match any overloaded call:
QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'Klose'
QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'Klose'
QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'Klose'
我是python和Qt的新手,我将非常感谢你的帮助。
编辑:
@pyqtSlot()
装饰器,任何方法都可以是Slot。 感谢大家: - )
答案 0 :(得分:5)
PyQt信号/槽语法与C ++语法不完全相同。
尝试:
class Klose:
def closingStuff(self):
print("bye")
...
app.connect(app, SIGNAL("aboutToQuit()"), k.closingStuff)
不确定在PyQt中是否有必要,但通常期望信号和插槽来自/转到QObjects。如果您的PyQt版本足够新,那么New-style signals and slots可能会引起您的兴趣。
答案 1 :(得分:0)
在PyQt5中,新式信号:app.aboutToQuit.connect(...)
def app_aboutToQuit():
print('app_aboutToQuit()')
app = QtWidgets.QApplication(sys.argv)
app.aboutToQuit.connect(app_aboutToQuit)