0

I am trying to get the value emitted by Class Qthread's signal without using the function

I am calling alert box when the user chooses to exit and press [X] button on GUI so I called this alert box by the means of threading to ensure that my GUI doesn't freeze.

Below is the code for that:

class ABC:
    def __init__(self):
             self.close = CloseThread() # Creating instance of QThread Class
........
........
........
    def closeEvent(self, event):
    button = self.close.CloseResult 
    self.close.start()
    if button == 1:
        event.accept()
    else:
        event.ignore()

class CloseThread(QThread):
"""Threading class which is emitting the return value according to the choice 
   of the user(When the message appears, if the user chooses 
   'Ok' then return value is 1 and if the user chooses
   'Cancel'return value is 2)"""
CloseResult = QtCore.pyqtSignal(object)

def __init__(self):
    QThread.__init__(self)

def close(self):
    button = win32api.MessageBox(0, 'Do you want to Exit?', 'Message')
    self.CloseResult.emit(button)

def run(self):
    self.close()

I know I can get the emitted value by self.close.CloseResult.connect(self.fn_name) instead of using self.close.CloseResult but I don't want to call another function because here I am extending the scope of closeEvent function so I don't want to call another function.(Actually by calling another function things aren't working out as they are supposed to)

Note: By using self.close.CLoseResult I am getting an object so is there any way to access this object's value or not?

So is there any way I can get emitted value just in a variable without having to call any other function?

Aadit
  • 199
  • 1
  • 6
  • 17
  • There is really no need for a thread here at all. Just show a normal `QMessageBox` in `closeEvent`. You **want** your application to stop at that point, so the user has the chance to choose what to do next. If you used a thread, the application would close before the user had a chance to act. – ekhumoro Oct 19 '17 at 22:44
  • @ekhumoro We meet again (Haha) My GUI freezes when the message appears(?), also I can use win32api too right and I am just getting the result from the thread the **if-else** is happening **outside the thread**? Say If I want to get the emitted value not without having to make any function is it possible? – Aadit Oct 19 '17 at 22:52
  • 1
    I have nothing to add to my previous comment. That is the standard way to do things, and there is no reason why it shouldn't work if you do it right. Using a thread is just plain wrong. – ekhumoro Oct 20 '17 at 00:05

0 Answers0