2016-04-25 42 views
0

我有一個應用程序繼承QtGui.QMainWindow窗體,並重新定義closeEvent以顯示MessageBox。PyQt - 單擊「退出」按鈕時顯示MessageBox

def closeEvent(self, event): 

    reply = QtGui.QMessageBox.question(
           self, 
           'Quit', 
           'Are you sure you want to quit?', 
           QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, 
           QtGui.QMessageBox.Yes) 

    if reply == QtGui.QMessageBox.Yes: 
     event.accept() 
    else: 
     event.ignore() 

當我點擊窗口中的'X'時,該MessageBox出現。該應用程序還具有「退出」按鈕。我試圖將按鈕連接到closeEvent的重定義,所以當我點擊MessageBox出現的按鈕時。但是,當我確認要退出時,我只能回到我的應用程序。

def create_components(self): 

    self.button = QtGui.QPushButton('Quit') 
    self.button.clicked.connect(self.button_quit) 

def button_quit(self): 

    self.status_bar.showMessage('Leaving Application') 
    # QtCore.QCoreApplication.instance().quit() 
    self.closeEvent(QtGui.QCloseEvent()) 

在 'create_components' 方法被稱爲在INIT

+1

'self.button.clicked.connect(self.close)'射出。 – ekhumoro

回答

3

呼叫self.close()closeEvent將被Qt

def button_quit(self): 
    self.status_bar.showMessage('Leaving Application') 
    self.close() 
+0

謝謝,這對我有用。 – jruota