I am trying to emit a signal when a python thread finish running so when it is done, the emtited signal close the PYQT window that started the thread.
in the code see comments.
import sys
import threading
from PyQt4 import QtGui, QtCore
class MyThread(threading.Thread):
""" docstring for MyThread
"""
def __init__(self, job_done, settings):
super(MyThread, self).__init__()
self._Settings = settings
def run(self):
while not job_done.is_set():
if not processSetting(self._Settings):
raise Exception("Could not process settings")
else:
## Emit Signal
pass
class MyWindow(QtGui.QWidget):
"""docstring for MyWindow"""
def __init__(self, settings, parent=None):
super(MyWindow, self).__init__(parent)
self._Settings = settings
self._job_done = threading.Event()
self._myThread = MyThread(self._job_done, self._Settings)
## catch the signal to close this window.
def closeEvent(self, event):
if self._myThread.isAlive():
reply=QtGui.QMessageBox.question(self, "Are you sure to quit?","Settings are getting applied !!!",QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
if reply==QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
def processSettings():
print "processSettings() Called"
return True
def main():
app = QtGui.QApplication(sys.argv)
main = MyWindow()
main.show()
sys.exit(app.exec_())
in the above code i want to signal when processSttingts returns True and then MyWindow should close.
EDIT here is what I tried to do.
So what I am heading towards is to emit a signal if the processSettings
returns True
and close the QMainWindow
MyWindow
.
import sys
import threading
from PyQt4 import QtGui, QtCore
def processSettings():
print "processSettings() Called"
return True
class MyThread(threading.Thread):
""" docstring for MyThread
"""
def __init__(self, settings):
super(MyThread, self).__init__()
self._Settings = settings
self.signal = QtCore.SIGNAL("signal")
self._job_done = threading.Event()
def run(self):
# while not job_done.is_set():
print "in thread"
if not processSettings():
raise Exception("Could not process settings")
else:
QtCore.emit(self.signal, "hi from thread")
class MyWindow(QtGui.QMainWindow):
"""docstring for MyWindow"""
def __init__(self, settings, parent=None):
super(MyWindow, self).__init__(parent)
self._Settings = settings
self._myThread = MyThread(self._Settings)
self._myThread.daemon = False
self._myThread.start()
self.connect(self._myThread, self._myThread.signal, self.testfunc)
def closeEvent(self, event):
if self._myThread.isAlive():
reply=QtGui.QMessageBox.question(self, "Are you sure to quit?","Settings are getting applied !!!",QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
if reply==QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
def testfunc(self, sigstr):
""" Purpose of this function is to close this window"""
print sigstr
self.close()
def main():
app = QtGui.QApplication(sys.argv)
settings = {'test': True}
wind = MyWindow(settings)
wind.show()
sys.exit(app.exec_())
if __name__ == '__main__':
sys.exit(main())
QtCore
i tried using self but that won't make sense sincethreading.Thread
object doesn't have emit function, so now all I need is to emit theQtSignal
– Ciasto piekarz