簡體   English   中英

通過模態對話框關閉PyQt App

[英]Close PyQt App via a Modal Dialog

我目前正在使用Qt4 python綁定對應用程序進行編碼,該綁定要求用戶提供登錄憑據。 在啟動應用程序時,我顯示一個模式對話框,用戶可以在其中輸入數據。 由於當用戶未登錄時應用程序無法提供有用的服務,因此如果用戶單擊“取消”按鈕,我想關閉該應用程序。

因此,如果對話框返回否定結果,則只需調用QMainWindowclose()方法。 通常,由於不再有可交互的窗口,因此這會導致應用程序退出。

但是,如果以前顯示了模式對話框,則該應用程序將繼續運行,而我必須手動將其殺死。

以下是一個最小示例的代碼

main.py:

import sys
from PyQt4.QtGui import QApplication
from MyMainWindow import MyMainWindow

app = QApplication(sys.argv)

window = MyMainWindow()
window.show()

app.exec_()

print "Terminated"

MyMainWindow.py:

from PyQt4.QtGui import QMainWindow
from PyQt4 import QtGui
from MyDialog import MyDialog

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.setWindowTitle("Close App Test")

        self.centralwidget = QtGui.QWidget(self)
        self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)

        self.closeButton = QtGui.QPushButton(self.centralwidget)
        self.closeButton.setText("Close")

        self.closeButton.clicked.connect(self.exit)

    def show(self):
        QMainWindow.show(self)

        myDialog = MyDialog(self) 
        res = myDialog.exec_()  

        if res == 0:
            self.exit()
        else:
            print "Continue"

    def exit(self):
        self.close()

MyDialog.py:

from PyQt4.QtGui import QDialog
from PyQt4 import QtGui

class MyDialog(QDialog):

    def __init__(self, parent = None):
        QDialog.__init__(self, parent)

        self.setWindowTitle("Modal Dialog")

        self.resize(200, 50)

        self.closeButton = QtGui.QPushButton(self)
        self.closeButton.setText("Close")
        self.closeButton.move(10, 10)

        self.otherButton = QtGui.QPushButton(self)
        self.otherButton.setText("Do Nothing")
        self.otherButton.move(100, 10)

        self.closeButton.clicked.connect(self.reject)
        self.otherButton.clicked.connect(self.accept)

如果單擊模式對話框上的“關閉”按鈕,則即使關閉了所有窗口,應用程序仍將繼續運行。 如果單擊“不執行任何操作”按鈕,並且使用主窗口上的“關閉”按鈕關閉了應用程序,那么一切都會按預期進行,並且該應用程序將終止。

我真的看不到這兩種情況之間的區別,因為每次我只在主窗口上調用close時。 我可以假設我在處理模式對話框時有錯誤。 我什至嘗試修改myDialog.close()myDialog.destroy()以及quitOnClosedeleteOnClose窗口屬性,而沒有任何積極影響。

任何幫助表示贊賞。

如果單擊模式對話框上的“關閉”按鈕,則在app.exec_()之前調用self.exit(),因此應用程序將繼續運行。 當主事件循環運行時,應調用self.exit()。

以下是這種情況的可能解決方案:

from PyQt4.QtGui import QMainWindow
from PyQt4 import QtGui
from pyQt4 import QtCore

from MyDialog import MyDialog

class MyMainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.setWindowTitle("Close App Test")

        self.centralwidget = QtGui.QWidget(self)
        self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)

        self.closeButton = QtGui.QPushButton(self.centralwidget)
        self.closeButton.setText("Close")

        self.closeButton.clicked.connect(self.exit)

    def login(self):
        myDialog = MyDialog(self) 
        res = myDialog.exec_()  

        if res == 0:
            self.exit()
        else:
            print "Continue"

    def show(self):
        QMainWindow.show(self)
        QtCore.QTimer.singleShot(0, self.login)        

    def exit(self):
        self.close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM