Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Exposing my c++ function to a Python script
Forum Updated to NodeBB v4.3 + New Features

Exposing my c++ function to a Python script

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 819 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ahmadinho
    wrote on 17 Jun 2020, 12:38 last edited by
    #1

    Hello all,

    Is there a way to expose c++ functions/objects to a python script and evaluate this script in my qt program, just like whats done with the java script engine?

    Thanks.

    J 1 Reply Last reply 17 Jun 2020, 12:45
    0
    • A Ahmadinho
      17 Jun 2020, 12:38

      Hello all,

      Is there a way to expose c++ functions/objects to a python script and evaluate this script in my qt program, just like whats done with the java script engine?

      Thanks.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 17 Jun 2020, 12:45 last edited by
      #2

      @Ahmadinho Do you mean you want to embed Python script in your C++ app? Like this: https://docs.python.org/3/extending/embedding.html (not Qt related)?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Ahmadinho
        wrote on 17 Jun 2020, 13:00 last edited by
        #3

        Hello jsulm, thanks for your help, yes something like that, however assume I have a function called add in my c++ code, i want the python script to be able to use that function.

        J S 2 Replies Last reply 17 Jun 2020, 13:04
        0
        • A Ahmadinho
          17 Jun 2020, 13:00

          Hello jsulm, thanks for your help, yes something like that, however assume I have a function called add in my c++ code, i want the python script to be able to use that function.

          J Offline
          J Offline
          JonB
          wrote on 17 Jun 2020, 13:04 last edited by JonB
          #4

          @Ahmadinho
          It's not very clear to me which way round you want access, Python -> C++, C++ -> Python, both.... In any case, if you read the very first sentence of the link @jsulm gave you it states:

          The previous chapters discussed how to extend Python, that is, how to extend the functionality of Python by attaching a library of C functions to it.

          So did you read that far?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Ahmadinho
            wrote on 17 Jun 2020, 13:12 last edited by
            #5

            Hi JonB, I did and it didnt seem to have a way to expose c++ objects as well, isnt there a qt module like the java script one?

            The main thing I want to do is, have for example in my c++ code the following: class A with function C D, in the python script i can initiate instances from Class A and call A.B and A.B.

            J J 2 Replies Last reply 17 Jun 2020, 13:14
            0
            • A Ahmadinho
              17 Jun 2020, 13:12

              Hi JonB, I did and it didnt seem to have a way to expose c++ objects as well, isnt there a qt module like the java script one?

              The main thing I want to do is, have for example in my c++ code the following: class A with function C D, in the python script i can initiate instances from Class A and call A.B and A.B.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 17 Jun 2020, 13:14 last edited by
              #6

              @Ahmadinho What about using PyQt or PySide?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • A Ahmadinho
                17 Jun 2020, 13:12

                Hi JonB, I did and it didnt seem to have a way to expose c++ objects as well, isnt there a qt module like the java script one?

                The main thing I want to do is, have for example in my c++ code the following: class A with function C D, in the python script i can initiate instances from Class A and call A.B and A.B.

                J Offline
                J Offline
                JonB
                wrote on 17 Jun 2020, 13:22 last edited by JonB
                #7

                @Ahmadinho said in Exposing my c++ function to a Python script:

                it didnt seem to have a way to expose c++ objects as well,

                You can do a Google on expose c++ object to python. There are a few approaches. It may be interesting to read one person's comment from https://intermediate-and-advanced-software-carpentry.readthedocs.io/en/latest/c++-wrapping.html#wrapping-c-c-for-python [my bold]:

                When I write wrappers for C and C++ code, I usually provide a procedural interface to the code and then use Python to construct an object-oriented interface. I do things this way for two reasons: first, exposing C++ objects to Python is a pain; and second, I prefer writing higher-level structures in Python to writing them in C++.

                1 Reply Last reply
                3
                • A Ahmadinho
                  17 Jun 2020, 13:00

                  Hello jsulm, thanks for your help, yes something like that, however assume I have a function called add in my c++ code, i want the python script to be able to use that function.

                  S Offline
                  S Offline
                  student
                  wrote on 17 Jun 2020, 13:34 last edited by
                  #8

                  @Ahmadinho I think pybind11 or swig can achieve what you want.

                  1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    Ahmadinho
                    wrote on 17 Jun 2020, 14:38 last edited by
                    #9

                    Thanks a lot @student, will look into them further.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      gde23
                      wrote on 17 Jun 2020, 14:53 last edited by gde23
                      #10

                      cython is also an possibility to wrap c/c++ code in python and its easier to set up than swig.

                      1 Reply Last reply
                      1

                      1/10

                      17 Jun 2020, 12:38

                      • Login

                      • Login or register to search.
                      1 out of 10
                      • First post
                        1/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved