mrfc
mrfc

Reputation: 23

PyQt4 window not properly closing with signals and slots, only "x" button

As the title suggests, when I create a push button, or a menu option for ending the program, the window doesn't close.

so I'm trying to figure out to end the program and close the window simultaneously. I've been using the tutorial:

http://zetcode.com/tutorials/pyqt4/

which is great otherwise. So how do i connect the push button with ending & closing a widget?

Here is some sample code (copied from the tutorial) that I have been using. I cant seem to get the ending to copy exactly, but I don't think that's the issue anyways:

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

This program creates a quit
button. When we press the button,
the application terminates. 

author: Jan Bodnar
website: zetcode.com 
last edited: October 2011
"""

import sys
from PyQt4 import QtGui, QtCore


class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):               

        qbtn = QtGui.QPushButton('Quit', self)
        qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        qbtn.resize(qbtn.sizeHint())
        qbtn.move(50, 50)       

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Quit button')    
        self.show()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Thanks!

Upvotes: 0

Views: 1280

Answers (3)

Karthick
Karthick

Reputation: 58

replace qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit) with qbtn.clicked.connect(self.close) I too faced the same problem (trying to learn Qt4 from the same zetcode tutorials) and came here to look for a solution

Upvotes: 0

danielMartinez
danielMartinez

Reputation: 11

The code works. I have the same issue, but running it outside IDLE it works. No need to change the code to call the quit method on the QtGui module.

Upvotes: 1

Ingrid
Ingrid

Reputation: 160

Your app is a QtGui.QApplication, so why are you connecting to the quit signal using QtCore.QCoreApplication? Changing that to QtGui.QApplication.instance().quit works.

Upvotes: 0

Related Questions