1
我已經得到這個問題幾個小時了。我調查了很多東西(我甚至創建了一個自定義QDialog),但現在我確定它是導致此問題的closeEvent和QMessageBox組合。我想知道的是,如果有什麼辦法可以解決這個問題?closeEvent和QMessageBox似乎阻止更新UI
問題:
我想讓我的代碼清理其所有使用的資源如關閉之前,線程,DLL文件等。實際完成整個清理需要幾秒鐘的時間。爲了確保應用程序運行正常的用戶,我想'打印'狀態消息,確認應用程序正在嘗試清理其資源。
但是,使用下面的代碼我可以得到的是「你確定.....」。 「停止應用程序.....」消息未插入到processEdit
中。
我的代碼片段,test.py:
import sys, os, time
from PySide.QtGui import *
from PySide.QtCore import *
from time import sleep
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(350, 100, 300, 300)
self.processEdit = QTextEdit()
self.grid = QGridLayout()
self.grid.addWidget(self.processEdit, 0, 0)
self.setLayout(self.grid)
self.show()
def closeEvent(self, event = False):
self.processEdit.insertHtml("\n\n Are you sure.....")
if isinstance(event, QCloseEvent):
event.ignore()
confirm = QMessageBox.question(self, 'Message', "Are you sure you want to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if confirm == QMessageBox.Yes:
self.processEdit.insertHtml("\n\n Stopping App.....")
# clean up resources and status reports here.
time.sleep(5) # only here for the snippet
event.accept()
else:
event.ignore()
if __name__ == '__main__':
qapp = QApplication(sys.argv)
c = MainWindow()
sys.exit(qapp.exec_())
重繪是多餘的,應用程序實例可以通過'QtGui.qApp'訪問。我同意這不是一個好主意,讓GUI在關閉期間不響應 - 也許最好是隱藏窗口和/或顯示閃屏。 – ekhumoro
太好了。我不知道'repaint()'函數。我確實計劃確保GUI始終是響應式的。睡眠只是..嗯..在片段上做的事情.. ..哈哈 再次感謝。 –
@kazemakase我已經接受了你的回答,但是我想知道你是否可以跟進你所說的話。 「通過在closeEvent回調中執行sleep/cleanup來阻塞事件循環」。在申請終止前是否有其他地方需要清理? (早些時候應該問這個問題,但我很高興克服它的工作)。 (嗯..我應該問另一個問題o_O) –