Menu

Passing a CPP class to a Python method

Help
2020-03-03
2020-03-05
  • Flower lady

    Flower lady - 2020-03-03

    Long story short, I have a python class TypeProcessor defined such that one of the methods takes a CustomType CPP class. I've wrapped up CustomType as per the PythonQt examples, and am having success simply calling other methods of TypeProcessor via calls to the call routine of the corresponding PythonQtObjectPtr. However, those methods are easy to call since they all take types that cast to QVariants out of the box, so I can do:

            QVariantList args = QVariantList() << argument;
            QVariant testOut = m_typeProcessor.call("method_name", args);
    

    So my question is simply how to pass my CustomType into the arguments list of the call routine. I get an error when I try:

            CustomType* myTypePtr = new CustomType();
            QVariantList args = QVariantList() << myTypePtr;
            QVariant testOut = m_typeProcessor.call("method_name", args);
    

    Any suggestions?

     
  • Flower lady

    Flower lady - 2020-03-04

    Any thoughts on this, folks? I'm tempted to just copy the data from my CustomType into a QJsonObject so I can pass that to Python, but it'd be much cleaner if I could avoid any unnecessary serialization.

     
  • Florian Link

    Florian Link - 2020-03-04

    I am the 'folks'... Well, this is not a very common use case but there are probably multiple solutions... If your CustomType was derived from QObject, you could just create a Qvariant from that pointer. But since it is a C++ only type, you need to get it through the Qvariant without losing the type information. I think you could register a CustomType POINTER meta type, probably PythonQt would then know the type by the meta type id and since it is a registered C++ class, it should know what to do.

    If that does not work, you can also use

    PythonQtObjectPtr obj;
    obj.setNewRef(PythonQt::priv()->wrapPtr(yourPtr, "CustomType"))
    QVariant v = obj.toVariant()
    

    in which case you do the conversion to Python yourself and pass the wrapper to your call.

     
  • Flower lady

    Flower lady - 2020-03-04

    Oh hello Florian! I guess you are the folks. Thanks a bunch for the suggestions, I'll be sure to try them out. Do you think PythonQt would recognize that I've wrapped the C++ class, à la PyCPPWrapperExample? If so, that'd be super swell.

    Thanks again.

     
  • Flower lady

    Flower lady - 2020-03-05

    Hi Florian, I can confirm that the wrapped class gets recognized. What a beautiful thing. Thanks so much!

     
    • Florian Link

      Florian Link - 2020-03-05

      The problem is QVariant, it can only transport registered meta types. So it
      can transport a PyObject, a PythonQtObjectPtr and a QObject, but not a
      custom C++ pointer, it would take it as void* and lose type information.
      So you either convert it first to something that QVariant will handle, or
      you register its pointer as a meta type, so that QVariant does not lose the
      type information.

      On Thu 5. Mar 2020 at 04:57, Flower lady fancyflowerlady@users.sourceforge.net wrote:

      Hi Florian, I can confirm that the wrapped class gets recognized. What a
      beautiful thing. Thanks so much!


      Passing a CPP class to a Python method


      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/>

       

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.