Menu

Error handling with PythonQt

Help
TimoS
2013-05-27
2013-05-28
  • TimoS

    TimoS - 2013-05-27

    Hi

    First of all: Thank you for PythonQt. Years ago we tried using PyQt for a commercial project and it was just too difficult to deploy and integrate together with all the other work we had to do. PythonQt is for sure much better if you just want to have Python scripting in your application.

    But, here my question: What is the best way to add error handling? For example: I added my own C++ class. When somebody calls one of the methods on this class with the wrong parameters, I want to stop the python execution (if this is part of a script) and give an error. Some of these methods communicate with a USB device and if something goes wrong, I also want to stop execution and return with an error.

    Any advice?

    Regards,
    Timo

     
  • Florian Link

    Florian Link - 2013-05-27

    PythonQt 2.1 supports std::exception on Qt slots. So you can throw a std::exception derived exception and will get a Python exception that you can handle/that stops Python from executing.

     
  • Chris Bevan

    Chris Bevan - 2013-05-28

    Hi Timo,

    For the application I'm working on, we wanted the option to set a variety of different types of exceptions, depending on what sort of problem came up. I implemented this by adding an optional callback function that could be used to check the application for any exception it wants to set.

    The idea is:

    • PythonQt calls a function in the app
    • The app sets a flag somewhere that says what its current exception is, and returns
    • PythonQt calls a callback function to check for the exception
    • The app checks its flag, sees the exception, calls the Python function to set the corresponding exception directly, clears its flag for next time, and returns the exception to PythonQt
    • PythonQt skips converting the return value, because it doesn't need to

    That may give you some more flexibility if that's useful for you. I've attached the patch.

    Cheers,

    • Chris
     
  • TimoS

    TimoS - 2013-05-28

    Ah, great! Thank you Florian and Chris. I will investigate the proposed options.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.