How to have pushbutton click event to connect to closeEvent? PyQt4

Question:

I would like to have two different methods for quitting the app:

  1. By pressing the default “X” button on the left top corner.
  2. Pressing custom QPushButton(“Quit”)

The event where “X” is pressed is connected to a closeEvent function:

def closeEvent(self, event):
    message = QtGui.QMessageBox.question(self, "quit", "Do you really want to quit?",     QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
    if message == QtGui.QMessageBox.Yes:
         event.accept()
     else:
         event.ignore() 

Is there a way to connect the push button’s clicked event to go through the exact same function in order to quit? I’ve tried:

self.quit.clicked.connect(self.closeEvent(QtCore.QCoreApplication.quit))

but as soon as I run the GUI, the first thing that pops up is the message box.

What am I doing wrong and how could I fix it?

EDIT 1:

I’ve also tried

self.quit.clicked.connect(self.closeEvent(QtCore.QCoreApplication.instance().quit))

but it doesn’t work either.

Asked By: Krin123

||

Answers:

Just connect to the window’s close slot:

    self.quit.clicked.connect(self.close)

Calling close() posts a close-event, which will subsequently be delivered to the window’s closeEvent handler.

Answered By: ekhumoro

Calling close() posts a close-event, which will subsequently be delivered to the window’s closeEvent handler.


but i’m still wrong

Answered By: Kevin Chen
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.