I am making a GUI in PyQt for user to create backup of huge data.
The GUI ( main thread ) is taking inputs from user. rsync command ( for backup ) is also being called in main thread hence the window is freezing.
Aim is to try qthread such that app runs without freezing.
My search material :
1 : https://www.youtube.com/watch?v=o81Q3oyz6rg. This video shows how to not freeze GUI by running other task in secondary thread. I've tried it and it works. But it does not help in running the command in worker thread.
Inspite of calling rsync in secondary thread, the gui still freezes. What am I doing wrong ?.
import sys
from PyQt4 import QtCore, QtGui
from backUpUi import Ui_MainWindow
import threading, Queue
class callerThread(QtCore.QThread):
def __init__(self, func, parent=None, *args, **kwargs):
super(callerThread, self).__init__(parent)
self._func = func
self._args = args
self._kwargs = kwargs
def run(self):
self._func(*self._args, **self._kwargs)
class Monitor(QtCore.QObject):
updateText = QtCore.pyqtSignal(str)
def update_list(self):
t_monitor = callerThread(self.monitor_vector, parent=self)
t_monitor.daemon = True
t_monitor.start()
def monitor_vector(self):
self.updateText.emit('updated list')
class backUpMain(QtGui.QMainWindow):
def __init__(self,parent=None):
super(backUpMain, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.connect(self.ui.okButton, QtCore.SIGNAL("clicked()"), self.startThread)
self.ui.cancelButton.released.connect(sys.exit)
self.monitor = Monitor()
def _handlebackUpdate(self, txt):
QtGui.QMessageBox.information(self, "thread started!", txt)
self.ui.logEdit.clear()
self.ui.logEdit.setText(txt)
def startThread(self):
self.monitor = Monitor()
self.monitor.updateText.connect(self._handlebackUpdate)
self.monitor.update_list()
def threadDone(self,text1):
self.ui.logEdit.append("Worker Thread finished processing %s" % text1)
def exitWindow(self):
self.ui.close()
def main():
app = QtGui.QApplication(sys.argv)
dialog = backUpMain()
dialog.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
While searching for answer, qtcentre helped with it.
This signal is used to communicate between threads.
to communicate from main thread to worker thread,
create instance of qthread in init of class where Ui is defined to pass parameters from main thread either in init or where required.
Hope this helps.