How to prevent QFileDialog to freeze the main thread

74 views Asked by At

I've a main GUI thread which starts async websockets in background. I need to open a QFileDialog to select a file but doing it from the main thread freezes everything until I've selected the file and the QFileDialog closes.

I've tried to open it in a separate task using loop.createTask but accessing a widget outside the main GUI thread is not supposed to be.

So how to open a QFileDialog which do not block the main GUI thread ?

Thanks Stephane

1

There are 1 answers

0
Stéphane REY On

I've found that opening the QFileDialog from a thread was preventing it to block the main GUI thread :

@pyqtSlot()
def open_file_dialog(self):
    file_name, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File', '', 'Session Files (*.cfg)')
    if file_name:
        self.worker_thread.file_selected_signal.emit(file_name)

def start_worker_thread(self):
    self.worker_thread = WorkerThread(self)
    self.worker_thread.request_file_dialog_signal.connect(self.open_file_dialog)
    self.worker_thread.file_selected_signal.connect(self.worker_thread.handle_file_selected)
    self.worker_thread.start()