我的应用程序只能通过右键单击托盘图标并按“退出”来退出:
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_()
我的意图是在应用程序即将退出时执行一段代码。
这是我得到的错误:
$ ./rsAg.py
Traceback (cuộc gọi gần đây nhất):
File "./rsAgent.py", line 20, in
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 的新手,非常感谢你的帮助。
biên tập:
- 我忘了说版本(python:3.2,pyQt:4.8.4)
- 我们不需要类来定义插槽。通过使用
@pyqtSlot()
装饰器,任何方法都可以是一个 Slot。
- 我在代码中添加了 noClassMethod()。
- @Mat,你的建议帮助我走得更远。现在我找到了其他 3 种方法。我猜这是关于旧式与新式của.
- 对于 future 可能的读者,我不会删除错误消息。
感谢大家:-)
PyQt 的信号/槽语法与 C++ 的并不完全相同。
thử:
class Klose:
def closingStuff(self):
print("bye")
...
app.connect(app, SIGNAL("aboutToQuit()"), k.closingStuff)
不确定在 PyQt 中是否有必要,但通常希望信号和槽来自/去往 QObjects。 New-style signals and slots如果您的 PyQt 版本足够新,您可能会感兴趣。
Tôi là một lập trình viên xuất sắc, rất giỏi!