Menu

How pass pointer from python to cpp

Help
Edvard
2018-09-09
2018-09-10
  • Edvard

    Edvard - 2018-09-09

    Hello
    I have problem with c function pointer wrapping. I have a c function that one of its argument is int pointer. so how can I pass pointer from python to it?

     
  • Florian Link

    Florian Link - 2018-09-09

    You can't. You could change the interface to take a QVector or QList of ints. The any sequence in Python will be converted to that list/vector.

     
  • Edvard

    Edvard - 2018-09-09

    thanks, I want a way that each value change of variable in cpp function also applies to python. is there any way to do that?

     
  • Florian Link

    Florian Link - 2018-09-09

    Hm, there are various ways, but all require some work, especially if you want to be able to modify the data from both sides...

    Maybe the easiest would be to use a Python array (see standard library) of the desired type. But PythonQt does not support automatic conversion, so you would need to use a PyObject pointer in your slot and use the Python C api to talk to it from C++.

    Another common way is to use numpy arrays, but that will require the numpy installation and you would need to use the numpy C api to get acces to the data in the C++ slot (also with a PyObject)

    And another way is to return a Python memory view and use that as a buffer for an aary or numpy array.

    But all these methods require deep understanding on what you are doing...keeping the ref counts correct etc.

     
    • Edvard

      Edvard - 2018-09-10

      thanks for helping

       

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.