Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with SIGNAL - SLOT, aboutToQuit()

My application exits only by right-clicking the tray icon, and pressing "Quit":

class DialogUIAg(QDialog):
    ...
    self.quitAction = QAction("&Quit", self, triggered=qApp.quit)

The Module below is the application's starting point:

#!/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_()

My intention is to execute a block of code, when the application is aboutToQuit.
This is the Error i am getting:

$ ./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'

I am new to python and Qt and i would appreciate your help.


EDIT:

  • I forgot to mention versions (python: 3.2, pyQt: 4.8.4)
  • We dont need a class to define a Slot. Any method can be a Slot, by using the @pyqtSlot() decorator.
  • I added noClassMethod() in the code.
  • @Mat , your suggestion helped me go further. Now i found 3 other ways of doing it. I guess its about old-style vs new-style.
  • I will not delete the Error message, for possible future readers.

Thanks to everyone :-)

like image 946
athspk Avatar asked Nov 30 '25 23:11

athspk


1 Answers

The PyQt signal/slot syntax isn't entirely identical to the C++ one.

Try with:

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

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

Not sure it is necessary in PyQt, but signals and slots are generally expected to come from/go to QObjects. New-style signals and slots could be of interest if your version of PyQt is recent enough.

like image 192
Mat Avatar answered Dec 02 '25 12:12

Mat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!