Menu

How to set methods like __repr__ from the C++ side?

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

    Flower lady - 2019-12-16

    Pretty much the title, is there a way to do this? The examples show how to wrap C++ classes with the new_Class and delete_Class syntax as slots, but is it possible to override other python built-in functions?

     
  • Florian Link

    Florian Link - 2019-12-17

    Yes, there are some default Qt slot names that are looked for, but you have to register that your class supports the feature using an enum in registerCppClass (and you need to use the method on PythonQt::priv(), it has more arguments. Have a look at the generated wrappers, e.g. QPoint to see this in action. Regarding repr, PythonQt always looks for py_toString() slots.

     
  • Flower lady

    Flower lady - 2019-12-19

    Thanks Florian, that's very helpful! Works like a charm.

     

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

      Florian Link - 2019-12-19

      Hm, sounds like the destructor of your wrapper has a problem , at least
      that is probably what the argument frame deletes on reset...
      Do you do a qRegisterMetaType in your code? Using just the register macro
      is not enough...
      On Thu 19. Dec 2019 at 07:10, Flower lady fancyflowerlady@users.sourceforge.net wrote:

      Thanks Florian, that's very helpful! I have __iadd__, __isub__, and
      __imul__ operators working, but I'm getting a heap corruption error when
      trying to implement __add__, after PythonQtArgumentFrame::reset(). Any
      ideas on what might be causing this? I'm returning a custom type as a
      value, as such:

      ~~~
      const Quaternion PyQuaternion::add(Quaternion * q, const Quaternion &
      q2)
      {
      return ((*q) + q2);
      }
      ~~~

      My custom type is fairly straightforward,
      ~~~
      class Quaternion: public Serializable {
      public:

      Quaternion();
      Quaternion(real_g x, real_g y, real_g z, real_g w);

      const Quaternion operator+(const Quaternion& q1, const Quaternion & q2)
      {
      return Quaternion(q1.m_x + q2.m_x, q1.m_y + q2.m_y, q1.m_z + q2.m_z,
      q1.m_w + q2.m_w);
      }

      protected:
      real_g m_x, m_y, m_z, m_w;
      };
      Q_DECLARE_METATYPE(Quaternion)
      Q_DECLARE_METATYPE(Quaternion*)
      ~~~

      And I register it as such:
      ~~~
      int typeFlag = int(
      PythonQt::Type_Add |
      PythonQt::Type_Subtract |
      PythonQt::Type_Multiply |
      PythonQt::Type_InplaceAdd |
      PythonQt::Type_InplaceSubtract |
      PythonQt::Type_InplaceMultiply |
      PythonQt::Type_RichCompare);

      PythonQt::self()->priv()->registerCPPClass("Quaternion",
          "Serializable",
          "alg",
          PythonQtCreateObject<PyQuaternion>,
          NULL,
          NULL,
          typeFlag);
      

      ~~~

      But calling the type via python as below causes sad times:
      ~~~
      from PythonQt.alg import Quaternion
      q = Quaternion(0.0, 0.1, 0.1, 1.0)
      q2 = q * q
      ~~~


      How to set methods like repr from the C++ side?


      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

    Hi Florian, sorry, I edited my post quickly but you found it before I did! There was a problem with the metatype registration, as you'd suggested. 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.