Menu

Unwrapping type from PyObject*

Help
2019-12-27
2019-12-27
  • Flower lady

    Flower lady - 2019-12-27

    Hi folks,

    Does anybody know of a way to obtain a custom type from a PyObject* representing that type, assuming it is registered with PythonQt and all that? e.g., Can I have the following:

    MyType* get_object(PyObject* o){
    ...
    }
    

    I would like to be able to do this because I can't cleanly overload my function to have something like:

    MyType* get_object(PyObject* o){
    ... Do stuff to o...
    MyType* myTypeO = toMyType(o); // How can I do this?
    return myTypeO;
    }
    MyType* get_object(MyType* o){
    ... Do stuff to o...
    return o;
    }
    

    With this overload in my CPP decorator class, PythonQt always seems to prefer to run the first method, even if the argument is of MyType*. I could simply not have a method version that takes a PyObject* argument to avoid the ambiguity, but then I'd be unable to pass in the Python types that I need.

     

    Last edit: Flower lady 2019-12-27
    • Florian Link

      Florian Link - 2019-12-27

      Have you tried changing the order of the slots? The more specific overload
      needs to come first, I think. But yes, you can do a type check for PythonQt
      wrappers and get the wrapped ptr from a wrapper. There is no example, but
      you can see it in PythonQtConversion and PythonQtClassInfo how to do this.

      On Fri 27. Dec 2019 at 06:02, Flower lady fancyflowerlady@users.sourceforge.net wrote:

      Hi folks,

      Does anybody know of a way to obtain a custom type from a PyObject*
      representing that type, assuming it is registered with PythonQt and all
      that? e.g., Can I have the following:

      MyType* get_object(PyObject* o){ ... }

      I would like to be able to do this because I can't cleanly overload my
      function to have something like:
      MyType* get_object(PyObject* o){ ... Do stuff to o... MyType* myTypeO = toMyType(o); // How can I do this? return myTypeO; } MyType* get_object(MyType* o){ ... Do stuff to o... return o; }
      With this overload in my CPP decorator class, PythonQt always seems to
      prefer to run the first method, even if the argument is of MyType*. I
      could simply not have a method version that takes a PyObject* argument to
      avoid the ambiguity, but then I'd be unable to pass in the Python types
      that I need.


      Unwrapping type from PyObject*


      Sent from sourceforge.net because you indicated interest in <
      https://sourceforge.net/p/pythonqt/discussion/631393/>

      To unsubscribe from further messages, please visit <
      https://sourceforge.net/auth/subscriptions/>

       
      • Flower lady

        Flower lady - 2019-12-27

        Thanks Florian, can't believe it was as simple as switching the order! I'll look into PythonQtConversion should the need arise, but I think I'm all set!

         
  • Flower lady

    Flower lady - 2019-12-27

    I should add that I've tried:

            MyType* myTypeO = (MyType*)PythonQt::self()->priv()->unwrapForeignWrapper("MyType", o);
    

    But unfortunately, this doesn't seem to work, since I don't have a foreign wrapper factory (I'm not even sure what that is).

    Thanks!

     

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.