I put a quit button on my PyQt5 application that needs to quit the app with exit code 0. I found QCoreApplication.quit()
that needs to quit the application. This method works but doesn't give exit code 0. It rather freezes my computer for a few seconds just before exiting with Process finished with exit code -1073741819 (0xC0000005)
which is obviously a memory error. The app only does this when it I quit it using the button I made, this doesnt happen when using alt+f4.
class MorePage(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent=parent)
button = QPushButton("X", self)
button.setGeometry(0,0,70,70)
button.clicked.connect(quit)
def quit(): QCoreApplication.quit()
Calling QCoreApplication.quit ()
is the same as calling QCoreApplication.exit (0)
. To quote from the qt docs: After this function has been called, the application leaves the main event loop and returns from the call to exec(). The exec() function returns returnCode, if that makes sense.
A different way to do it would be to perhaps use sys.exit()
However, it is considered to be a bad idea in the case of Qt. Still, you can try this:
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
import sys
class PushButton(QWidget):
def __init__(self):
super(PushButton,self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("PushButton")
self.setGeometry(400,400,300,260)
button = QPushButton("X", self)
button.setGeometry(0,0,70,70)
button.clicked.connect(sys.exit)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = PushButton()
ex.show()
sys.exit(app.exec_())
You could also use QApplication.quit()
along with self.close()
to quit the application AND close the window, which would be a much better idea then sys.exit()
Try this:
def initUI(self):
self.setWindowTitle("PushButton")
self.setGeometry(400,400,300,260)
button = QPushButton("X", self)
button.setGeometry(0,0,70,70)
button.clicked.connect(self.quit)
def quit(self):
QApplication.quit()
self.close()
User contributions licensed under CC BY-SA 3.0