2011-06-29 56 views
2

我的申請通過右擊托盤圖標,然後按「退出」僅退出:問題的SIGNAL - SLOT,aboutToQuit()

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

我的意圖是執行一個代碼塊,當應用程序是aboutToQuit。
這是我收到的錯誤:

$ ./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,我會感謝你的幫助。


編輯:

  • 我忘了提版本(蟒蛇:4.8.4:3.2,PyQt的)
  • 我們不需要一個類來定義一個插槽。任何方法都可以通過使用裝飾器@pyqtSlot()來實現。
  • 我在代碼中添加了noClassMethod()。
  • @Mat,你的建議幫助我走得更遠。現在我發現了其他3種方式。我想它的約舊式和新式
  • 我不會刪除錯誤消息,以供未來的讀者閱讀。

感謝大家:-)

回答

5

PyQt的信號/槽語法不完全等同於C++之一。

嘗試:

class Klose: 
    def closingStuff(self): 
    print("bye") 

... 
app.connect(app, SIGNAL("aboutToQuit()"), k.closingStuff) 

不知道它是在PyQt的必要的,但信號和槽一般預計將有來自/去的QObject。如果你的PyQt版本足夠近,那麼你可能會感興趣的是New-style signals and slots

+2

在PyQt的,狹槽可以是Python函數(類是沒有必要)。文檔:http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/old_style_signals_slots.html#connecting-signals-and-slots – pedrotech

0

在PyQt5,新的風格的信號:app.aboutToQuit.connect(...)

def app_aboutToQuit(): 
    print('app_aboutToQuit()') 

app = QtWidgets.QApplication(sys.argv) 
app.aboutToQuit.connect(app_aboutToQuit) 
+0

這也適用於PyQt4,例如'app = QtGui.QApplication.instance( ); app.aboutToQuit.connect(app_aboutToQuit)' – dbr