0

I am trying to write a python macro for libre office 7.4 on windows 10 that creates a dialog with pyqt6.

    def open_qt_dialog():
        import sys

        from PyQt6.QtWidgets import (
            QApplication,
            QWidget,
        )

        app = QApplication([])
        window = QWidget()
        window.setWindowTitle("Simple Dialog")

        window.show()
        sys.exit(app.exec())

Regardless of whether I use a QDialog or a QMainWindow, I get the following error when I press the "X" button in the title bar. After that the dialog is closed.

    com.sun.star.uno.RuntimeException: <class 'SystemExit'>: 0, traceback follows
    File "C:\Program Files\LibreOffice\program\pythonscript.py", line 915, in invoke
    ret = self.func( *args )
  File "C:\Users\<username>\AppData\Roaming\LibreOffice\4\user\Scripts\python\Test.py", line 101, in open_qt_dialog
    sys.exit(app.exec())

If I then run the macro again, the dialog is called again and shortly after that libre office crashes. This behavior occurs regardless of whether the macro is executed via the menu or via a button.

Only closing the dialog seems to be a problem. Interaction with libre office (e.g. writing a value into a cell, when pressing a button) works so far. I already tried to overwrite the closeEvent method using the quit method on the mainwindow. But it just closed (or crashed) the whole libre office application. Do i mess anything? Do I still have to adjust the sys.exit command in any way? I have the suspicion that sys.exit tries to close libre office completely and not the dialog itsself.

If I run the script via pycharm (connected to uno via uno bridge), then I can close the dialog without any problems.

5
  • 1
    Try removing sys.exit() and just call app.exec(). Commented Feb 2, 2023 at 19:00
  • Are you aware of LibreOffice UNO dialogs? They work well from python macros and are cross-platform. But perhaps you require Qt dialogs for interface with another codebase or something.
    – Jim K
    Commented Feb 2, 2023 at 19:08
  • @musicamante I tried just using app.exec(). Now the com.sun.star.uno.RuntimeException Dialog is gone but LO still crashes after the second execution of the script.
    – Dukaaza
    Commented Feb 3, 2023 at 6:31
  • That's probably because you're trying to create a new QApplication instance while another one still exists. Try changing to app = QApplication.instance() and add if not app: app = QApplication() right after that. Also try to add del app after app.exec(). Commented Feb 3, 2023 at 6:51
  • @musicamante I tried that but it leads to other errors. At the end i tried using PyQt5 and the closing works without crashing or error dialogs using your hint removing sys.exit(). Thanks for your effort.
    – Dukaaza
    Commented Feb 6, 2023 at 6:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.