0

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?

d33tah
  • 10,999
  • 13
  • 68
  • 158

2 Answers2

2

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

Community
  • 1
  • 1
Rhys
  • 4,926
  • 14
  • 41
  • 64
  • 1
    +1: this is the answer. The questioner's code creates two windows: one from `self.ui` which is shown, and one created in the code at the bottom which is never shown. The questioner's code doesn't work because it is trying to close the window that was never opened. – Luke Woodward May 25 '13 at 08:53
0

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.

Chris
  • 17,119
  • 5
  • 57
  • 60
  • Unfortunately, that didn't help. I tried both commenting out the closeEvent and calling the QMainWindow's one and the window survived both of the attempts. – d33tah Apr 01 '13 at 16:18
  • Weird.. are you actually seeing your debug output when you override the closeEvent? – Chris Apr 01 '13 at 16:36
  • Yes I am. Except for the "never entered this". – d33tah Apr 01 '13 at 17:07