Main Gui Thread Freezes after using a QDialog with Exec_()

49 Views Asked by At

I shared the summary of my code below and I should state that dialogPopup is an instance of Ui_Dialog.The problem is when I press Ok or Cancel, the popup closes as expected and I get the result_code and do my computations afterwards; but it takes a while (about 5 seconds ) for the gui to respond to user actions for a 5 min duration of the popup having been untouched waiting for the user to press OK or Cancel.I tried build the same logic with EventLoop and emit of result_code by signal mechanishm with no success on freeze issue.I used accept and reject of Dialog but still the freeze takes place after close of the popup.

v1...

class MyModalDialog(QtWidgets.QDialog):
    def __init__(self,parent):
        super().__init__(),
        self._parent = parent
    
    def close_dialog(self, result_code):
        if result_code == 1:
            self._parent.dialogAnswer = 1
            self.dialogReturnOK()
        else:
            self._parent.dialogAnswer = 0
            self.dialogReturnCancel()
    
    
    def dialogReturnOK(self):
        self._parent.log("OK clicked..", basic=True)
        self.destroyPopup()
    
    def dialogReturnCancel(self):
        self._parent.log("Cancel clicked..", basic=True)
        self.destroyPopup()
    
    def destroyPopup(self):
        self.dialogPopupExec.close()
        self.dialogPopupExec.deleteLater()
    
    def open_modal_dialog(self):
        self.dialogPopup.cancel_button.pressed.connect(lambda: self.dialogPopupExec.close_dialog(0))
        self.dialogPopup.ok_button.pressed.connect(lambda: self.dialogPopupExec.close_dialog(1))
        self.dialogPopupExec.exec_()

self.dialogPopup = dialogPopup() 
self.dialogPopupExec = MyModalDialog(self) self.dialogPopupExec.setWindowFlags(QtCore.Qt.FramelessWindowHint) self.dialogPopup.setupUi(self.dialogPopupExec)

self.open_modal_dialog()

if self.dialogAnswer: 
    return "OK" 
return "Cancel"

v2....

class MyModalDialog(QtWidgets.QDialog):
    closed_with_result = pyqtSignal(int) 
    result_code = None
    def __init__(self, parent):
        super().__init__(),
        self._parent = parent
    
    def close_dialog(self, result_code):
        if result_code == 1:
            self._parent.dialogReturnOK()
        else:
            self._parent.dialogReturnCancel()
        self.result_code = result_code
        self.closed_with_result.emit(result_code)   
    
    def open_modal_dialog(self):
        self.dialogPopup.cancel_button.pressed.connect(lambda: self.dialogPopupExec.close_dialog(0))
        self.dialogPopup.ok_button.pressed.connect(lambda: self.dialogPopupExec.close_dialog(1))
        loop = QEventLoop()
        self.dialogPopupExec.closed_with_result.connect(loop.quit)  # Exit the loop when the dialog is closed
        self.dialogPopupExec.open()
        loop.exec_()    
        self.dialogAnswer = self.dialogPopupExec.result_code
        print("Dialog closed with result code:", self.dialogAnswer)
    
self.dialogPopup = dialogPopup() self.dialogPopupExec = MyModalDialog(self) self.dialogPopupExec.setWindowFlags(QtCore.Qt.FramelessWindowHint) self.dialogPopup.setupUi(self.dialogPopupExec)

self.open_modal_dialog()

if self.dialogAnswer: 
    return "OK"
return "Cancel"

I mean the user actions can't be grabbed immediately but after a delay of 5 to 10 seconds so the user may close the program by misthinking of a hang or crash.I exactly want the main gui thread to take control of the program after closal of any popup so the user can continue operations in a smooth manner

0

There are 0 best solutions below