I've finally found the solution, it was indeed the eventloop. The process will only be destroyed after the next loop.
match_manager.py
# Mettre à jour les variables dans le fichier Excel
ExcelToPdfWorker.update_variables(excel_path, variables)
# Connecter le signal avant de décharger le PDF
match_widget.editor.pdf_unloaded.connect(
lambda: self._convert_after_unload(match_widget, excel_path)
)
match_widget.editor.unload_pdf()
def _convert_after_unload(self, match_widget, excel_path):
"""Appelé quand le PDF est vraiment déchargé."""
self.excel_worker.excel_path = excel_path
self.excel_worker.convert()
# Une fois la conversion terminée, on peut recharger le PDF
match_widget.editor.load_new_pdf(match_widget.pdf_path)
editor.py
def unload_pdf(self):
"""Ferme le PDF actuel et nettoie les références."""
# Détacher la vue du document actuel
self.pdfView.setDocument(None)
# Fermer et supprimer l'ancien document
if self.pdfDocument:
old_document = self.pdfDocument
# Créer un nouveau document vide avant de détruire l'ancien
self.pdfDocument = QPdfDocument(self)
# Connecter le signal destroyed à l'émission de notre signal
old_document.destroyed.connect(self.pdf_unloaded.emit)
old_document.close()
old_document.deleteLater()
def load_new_pdf(self, pdf_path):
"""Charge un nouveau fichier PDF."""
# Charger le nouveau PDF
self.pdfDocument.load(pdf_path)
self.pdfView.setDocument(self.pdfDocument)
print(f"Nouveau PDF chargé: {pdf_path}")
return True
I'm well aware that my code sucks.
Here's the solution. As deleteLater() really destroy the PdfDocument and unload it, you have to wait until the end of a complete cycle and return to app.exec().
Then I put in a signal that will convert the file once the reference has been destroyed, since we know that this frees access
You should know that I think this is the only method without making temporary files