d33tah
d33tah

Reputation: 11561

QMainWindow.close doesn't close the window despite event.accept on closeEvent

I'm trying to write an application that utilizes a QMainWindow and has a QMenuBar there with File->Exit functionality, which also uses UIC files. I've stripped my project down to the part that doesn't work despite my efforts - closeEvent is called, it gets accepted, but the window doesn't close. Here's my test.py:

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

from __future__ import print_function

import sys
from PyQt4 import QtCore, QtGui, uic

class TruEdit(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QDialog.__init__(self)

        self.ui = uic.loadUi("test.ui")
        self.ui.show()

        self.ui.actionWyj_cie.triggered.connect(self.wyjscie)

    def wyjscie(self):
        self.close()

    def closeEvent(self, event):
        event.accept()
        print("WTF, still alive")

    @QtCore.pyqtSlot()
    def reject(self):
        print("Never entered this")
        return None


if __name__=="__main__":
    app = QtGui.QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(True)
    window = TruEdit()
    sys.exit(app.exec_())

And here's the test.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>

  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>21</height>
    </rect>
   </property>

   <widget class="QMenu" name="menuPlik">
    <property name="title">
     <string>Plik</string>
    </property>
    <addaction name="actionWyj_cie"/>
   </widget>
   <addaction name="menuPlik"/>
  </widget>
  <widget class="QStatusBar" name="statusbar">
   <property name="statusTip">
    <string/>
   </property>
  </widget>
  <action name="actionWyj_cie">
   <property name="text">
    <string>Wyjście</string>
   </property>
   <property name="shortcut">
    <string>Ctrl+K</string>
   </property>
  </action>
 </widget>
 <resources/>
 <connections/>
</ui>

What have I done wrong?

Upvotes: 0

Views: 1703

Answers (2)

Rhys
Rhys

Reputation: 5278

You might find a better answer here

PyQt: clicking X doesn't trigger closeEvent

It seems the problem lies within,

    self.ui = uic.loadUi("test.ui")
    self.ui.show()

Where an instance is being created called self.ui

Upvotes: 3

Chris
Chris

Reputation: 17535

It appears to me that you're overriding the closeEvent, and then doing nothing inside of it. Calling event.accept() doesn't actually execute the event. It just tells the event object that it's been accepted so that the event doesn't continue to propagate. In C++ land you would need to do something like this

void closeEvent(QCloseEvent *Event) {
    // do some stuff
    QMainWindow::closeEvent(event);
}

Note the call to QMainWindow's close event, which is where the code for actually closing the window is located.

Upvotes: 0

Related Questions