Ich habe ein kleines Problem. Und zwar entwickele ich gerade eine kleine Chat-Applikation mit PyQt4 und Python 2.6.
Ich habe ein Hauptfenster, welches die Kontaktliste darstellt (mit einem QListWidget). Klickt man auf einen Kontakt, öffnet sich ein Child-Fenster, in dem dann gechattet werden können soll. Das funktioniert alles soweit.
Jetzt möchte ich gerne ermitteln, wann eines dieser Child-Fenster geschlossen wird.
Code: Alles auswählen
class MainWindow(QtGui.QMainWindow):
''' Main window of the application. From here, other windows
are initiated. '''
def __init__(self, window_title="Title", parent=None):
QtGui.QMainWindow.__init__(self, parent)
# ...
Innerhalb des Child-Fensters habe ich hierzu closeEvent() definiert. Allerdings wird die Methode nicht ausgeführt (s.u.), wenn ich dieses Chatfenster schließe, sondern erst wenn ich das Kontaktlistenfenster und damit die Applikation schließe. Ich verstehe, nicht, wieso und konnte dazu auch keine Hinweise finden. Hat jemand eine Idee?
Code: Alles auswählen
class ChatWindow(QtGui.QMainWindow, Observed):
''' Child-window of the main-window. Is drawn when the user or
one of his contacts initiates a chat-session. '''
open_windows = []
def __init__(self, parent=None, id_=''):
QtGui.QMainWindow.__init__(self, parent)
self.id_ = id_
self.setWindowTitle(self.id_)
ChatWindow.open_windows.append(self.id_)
# ...
def update(self, new_data):
''' Called by the XMPP-class when new data arrives.
Attaches a new message to the textfield. '''
self.message_output.append('{0}: {1}'.format(new_data[0],
new_data[1]))
def closeEvent(self, event):
''' Called by QObject if the window is about to be closed. '''
self.close_window()
event.accept()
def close_window(self):
''' Remove the window from the windowlist and close the
window afterwards. '''
# TODO: Wird aufgerufen wenn das Hauptfenster geschlossen wird, nicht
# wenn ChatWindow geschlossen wird. WARUM?!
print("aufgerufen:", end=str(self.id_))
if self.id_ in ChatWindow.open_windows:
ChatWindow.open_windows.remove(self.id_)
# Prevent deletion of an empty object.
if ChatWindow.open_windows is None:
ChatWindow.open_windows = []
self.close()
Beste Grüße von Klip