I want to show loading progress via a modal QDialog
. So I create a thread to load the data and call exec()
on the dialog.
loading_progress_dialog = LoadingProgressDialog(len(filenames))
loadingWorker = analyzer.LoadingWorker(filenames, loading_progress_dialog.apply_progress)
workingThread = QThread()
workingThread.started.connect(loadingWorker.process)
loadingWorker.finished.connect(workingThread.quit)
workingThread.finished.connect(loading_progress_dialog.accept)
loadingWorker.moveToThread(workingThread)
workingThread.start()
loading_progress_dialog.exec()
I want the dialog to be responsible but it freezes and I'm not able to move it around on the screen while the loading thread is running.
class LoadingProgressDialog(QLoadingProgressDialog, Ui_LoadingDialog):
def __init__(self, maxFiles):
super(LoadingProgressDialog, self).__init__()
self.setupUi(self)
self.progressBar.setMaximum(maxFiles)
self.setWindowTitle('Loading files...')
def apply_progress(self, delta_progress):
self.progressBar.setValue(delta_progress + self.progressBar.value())
class LoadingWorker(QtCore.QObject):
def __init__(self, file_names, progress_made):
super(LoadingWorker, self).__init__()
self._file_names = file_names
self._progress_made = progress_made
finished = QtCore.pyqtSignal()
def process(self):
print("Thread started")
# load_csv_data(self._file_names, self._progress_made)
QtCore.QThread.sleep(5)
self.finished.emit()
Am I fighting with GIL or is it another problem? And second thing I am worried about is race-condition between self.finished.emit()
and loading_progress_dialog.exec()
. If the working thread is finished faster than gui thread runs exec()
, the dialog will not close. Is there any way to ensure that everything is in right order?