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. embed a python function into my Qt App.
Forum Updated to NodeBB v4.3 + New Features

embed a python function into my Qt App.

Scheduled Pinned Locked Moved Solved General and Desktop
32 Posts 4 Posters 7.0k 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.
  • jsulmJ jsulm

    @vinaygopal Please take a look at https://doc.qt.io/qt-5/qmake-variable-reference.html#libs

    V Offline
    V Offline
    vinaygopal
    wrote on last edited by
    #6

    @jsulm i have done the changes

    INCLUDEPATH +="C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include"
    
    wind32:LIBS += C:/Users/vinay/AppData/Local/Programs/Python/Python37-32/libs
    
    
    
    

    i have done the changes as per the document my include file works that is python.h is found but it is not able to link to the library as the path specified in the LIBS is a the directory to a bunch of libraries in python. i get this warning at the top of the screen

    "the code model could not parse an input file,which might lead to incorrect code completion"

    jsulmJ 1 Reply Last reply
    0
    • V vinaygopal

      @jsulm i have done the changes

      INCLUDEPATH +="C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include"
      
      wind32:LIBS += C:/Users/vinay/AppData/Local/Programs/Python/Python37-32/libs
      
      
      
      

      i have done the changes as per the document my include file works that is python.h is found but it is not able to link to the library as the path specified in the LIBS is a the directory to a bunch of libraries in python. i get this warning at the top of the screen

      "the code model could not parse an input file,which might lead to incorrect code completion"

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #7

      @vinaygopal said in embed a python function into my Qt App.:

      "the code model could not parse an input file,which might lead to incorrect code completion"

      You can ignore this.

      Can you show the exact linker error?

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

      V 2 Replies Last reply
      1
      • jsulmJ jsulm

        @vinaygopal said in embed a python function into my Qt App.:

        "the code model could not parse an input file,which might lead to incorrect code completion"

        You can ignore this.

        Can you show the exact linker error?

        V Offline
        V Offline
        vinaygopal
        wrote on last edited by
        #8
        This post is deleted!
        1 Reply Last reply
        0
        • jsulmJ jsulm

          @vinaygopal said in embed a python function into my Qt App.:

          "the code model could not parse an input file,which might lead to incorrect code completion"

          You can ignore this.

          Can you show the exact linker error?

          V Offline
          V Offline
          vinaygopal
          wrote on last edited by
          #9

          @jsulm these are the errors i get

          this is my main fail

          #include <QApplication>
          #include <QDebug>
          #include <Python.h>
          
          
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              qDebug()<<"working"<<endl;
          
          
              return a.exec();
          

          these are the errors

          C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include\pytime.h:6: In file included from ..\..\..\AppData\Local\Programs\Python\Python37-32\include/pytime.h:6:0,
          
          C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include\Python.h:87: from ..\..\..\AppData\Local\Programs\Python\Python37-32\include/Python.h:87,
          
          C:\Users\vinay\Documents\first_qml_training\example_project\main.cpp:6: from ..\example_project\main.cpp:6:
          
          C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include\object.h:448: error: expected unqualified-id before ';' token
               PyType_Slot *slots; /* terminated by slot==0. */
          
                             ^
          
          jsulmJ 1 Reply Last reply
          0
          • V vinaygopal

            @jsulm these are the errors i get

            this is my main fail

            #include <QApplication>
            #include <QDebug>
            #include <Python.h>
            
            
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                qDebug()<<"working"<<endl;
            
            
                return a.exec();
            

            these are the errors

            C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include\pytime.h:6: In file included from ..\..\..\AppData\Local\Programs\Python\Python37-32\include/pytime.h:6:0,
            
            C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include\Python.h:87: from ..\..\..\AppData\Local\Programs\Python\Python37-32\include/Python.h:87,
            
            C:\Users\vinay\Documents\first_qml_training\example_project\main.cpp:6: from ..\example_project\main.cpp:6:
            
            C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include\object.h:448: error: expected unqualified-id before ';' token
                 PyType_Slot *slots; /* terminated by slot==0. */
            
                               ^
            
            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #10

            @vinaygopal This is not linker error but compiler.
            Try to include the header like this:

            extern "C"
            {
              #include <Python.h>
            }
            

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

            V 1 Reply Last reply
            1
            • jsulmJ jsulm

              @vinaygopal This is not linker error but compiler.
              Try to include the header like this:

              extern "C"
              {
                #include <Python.h>
              }
              
              V Offline
              V Offline
              vinaygopal
              wrote on last edited by
              #11

              @jsulm I tried it and i got the same set of errors however when i tried with the right click and add library method i get this following error

              :-1: error: No rule to make target 'C:/Users/vinay/Documents/first_qml_training/example_project/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37d.a', needed by 'debug/example_project.exe'.  Stop.
              
              jsulmJ 1 Reply Last reply
              0
              • V vinaygopal

                @jsulm I tried it and i got the same set of errors however when i tried with the right click and add library method i get this following error

                :-1: error: No rule to make target 'C:/Users/vinay/Documents/first_qml_training/example_project/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37d.a', needed by 'debug/example_project.exe'.  Stop.
                
                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #12

                @vinaygopal Can you show your pro file?

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

                V 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @vinaygopal Can you show your pro file?

                  V Offline
                  V Offline
                  vinaygopal
                  wrote on last edited by
                  #13

                  @jsulm

                  QT       += core gui networkauth
                  
                  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                  
                  TARGET = example_project
                  TEMPLATE = app
                  
                  
                  # The following define makes your compiler emit warnings if you use
                  # any feature of Qt which has been marked as deprecated (the exact warnings
                  # depend on your compiler). Please consult the documentation of the
                  # deprecated API in order to know how to port your code away from it.
                  DEFINES += QT_DEPRECATED_WARNINGS
                  
                  # You can also make your code fail to compile if you use deprecated APIs.
                  # In order to do so, uncomment the following line.
                  # You can also select to disable deprecated APIs only up to a certain version of Qt.
                  #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                  
                  CONFIG += c++11
                  
                  SOURCES += \
                          main.cpp \
                          google_widget.cpp
                  
                  HEADERS += \
                          google_widget.h
                  
                  FORMS += \
                          google_widget.ui
                  
                  # Default rules for deployment.
                  qnx: target.path = /tmp/$${TARGET}/bin
                  else: unix:!android: target.path = /opt/$${TARGET}/bin
                  !isEmpty(target.path): INSTALLS += target
                  
                  DISTFILES += \
                      upload_to_cloud.py
                  
                  
                  #CONFIG += no_lflags_merge
                  
                  INCLUDEPATH +="C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include"
                  
                  
                  
                  win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/ -lpython37
                  else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/ -lpython37d
                  else:unix: LIBS += -L$$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/ -lpython37
                  
                  
                  win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37.a
                  else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37d.a
                  else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/python37.lib
                  else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/python37d.lib
                  else:unix: PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37.a
                  
                  
                  jsulmJ 1 Reply Last reply
                  0
                  • V vinaygopal

                    @jsulm

                    QT       += core gui networkauth
                    
                    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                    
                    TARGET = example_project
                    TEMPLATE = app
                    
                    
                    # The following define makes your compiler emit warnings if you use
                    # any feature of Qt which has been marked as deprecated (the exact warnings
                    # depend on your compiler). Please consult the documentation of the
                    # deprecated API in order to know how to port your code away from it.
                    DEFINES += QT_DEPRECATED_WARNINGS
                    
                    # You can also make your code fail to compile if you use deprecated APIs.
                    # In order to do so, uncomment the following line.
                    # You can also select to disable deprecated APIs only up to a certain version of Qt.
                    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                    
                    CONFIG += c++11
                    
                    SOURCES += \
                            main.cpp \
                            google_widget.cpp
                    
                    HEADERS += \
                            google_widget.h
                    
                    FORMS += \
                            google_widget.ui
                    
                    # Default rules for deployment.
                    qnx: target.path = /tmp/$${TARGET}/bin
                    else: unix:!android: target.path = /opt/$${TARGET}/bin
                    !isEmpty(target.path): INSTALLS += target
                    
                    DISTFILES += \
                        upload_to_cloud.py
                    
                    
                    #CONFIG += no_lflags_merge
                    
                    INCLUDEPATH +="C:\Users\vinay\AppData\Local\Programs\Python\Python37-32\include"
                    
                    
                    
                    win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/ -lpython37
                    else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/ -lpython37d
                    else:unix: LIBS += -L$$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/ -lpython37
                    
                    
                    win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37.a
                    else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37d.a
                    else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/python37.lib
                    else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/python37d.lib
                    else:unix: PRE_TARGETDEPS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/libpython37.a
                    
                    
                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #14

                    @vinaygopal On Windows it should be:

                    LIBS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/python37.lib
                    

                    See https://doc.qt.io/qt-5/qmake-variable-reference.html#libs

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

                    V 1 Reply Last reply
                    2
                    • jsulmJ jsulm

                      @vinaygopal On Windows it should be:

                      LIBS += $$PWD/../../../AppData/Local/Programs/Python/Python37-32/libs/python37.lib
                      

                      See https://doc.qt.io/qt-5/qmake-variable-reference.html#libs

                      V Offline
                      V Offline
                      vinaygopal
                      wrote on last edited by
                      #15

                      @jsulm i did add the library as you had recomended it still did'nt work and more importantly i figured the module python.h and its equivalent library is not compatible with mingw gcc compiler it is only compatible with microsoft specific compilers as suggested by this question (https://stackoverflow.com/questions/6731100/link-to-python-with-mingw) they have provided an answer but is there a simpler way to get this done?

                      jsulmJ 1 Reply Last reply
                      0
                      • V vinaygopal

                        @jsulm i did add the library as you had recomended it still did'nt work and more importantly i figured the module python.h and its equivalent library is not compatible with mingw gcc compiler it is only compatible with microsoft specific compilers as suggested by this question (https://stackoverflow.com/questions/6731100/link-to-python-with-mingw) they have provided an answer but is there a simpler way to get this done?

                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #16

                        @vinaygopal One question: do you build your app as 64bit? Your Python seems to be 32bit.

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

                        V 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @vinaygopal One question: do you build your app as 64bit? Your Python seems to be 32bit.

                          V Offline
                          V Offline
                          vinaygopal
                          wrote on last edited by
                          #17

                          @jsulm i am building my apps in the 64 bit configuration. shoudl i change it to 32 bit and try building the program again?

                          jsulmJ 1 Reply Last reply
                          0
                          • V vinaygopal

                            @jsulm i am building my apps in the 64 bit configuration. shoudl i change it to 32 bit and try building the program again?

                            jsulmJ Online
                            jsulmJ Online
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #18

                            @vinaygopal said in embed a python function into my Qt App.:

                            shoudl i change it to 32 bit

                            Yes, you can't mix 32bit and 64bit

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

                            V 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @vinaygopal said in embed a python function into my Qt App.:

                              shoudl i change it to 32 bit

                              Yes, you can't mix 32bit and 64bit

                              V Offline
                              V Offline
                              vinaygopal
                              wrote on last edited by
                              #19

                              @jsulm uninstalled python 32 bit and reinstalled 64bit version of python and included the libraries still the same error

                              :-1: error: No rule to make target 'C:/Users/vinay/Documents/first_qml_training/example_project/../../../AppData/Local/Programs/Python/Python38/libs/libpython38d.a', needed by 'debug/example_project.exe'. Stop.

                              jsulmJ 1 Reply Last reply
                              0
                              • V vinaygopal

                                @jsulm uninstalled python 32 bit and reinstalled 64bit version of python and included the libraries still the same error

                                :-1: error: No rule to make target 'C:/Users/vinay/Documents/first_qml_training/example_project/../../../AppData/Local/Programs/Python/Python38/libs/libpython38d.a', needed by 'debug/example_project.exe'. Stop.

                                jsulmJ Online
                                jsulmJ Online
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #20

                                @vinaygopal Can you show your current pro file?
                                Also did you a clean rebuild?

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

                                V 1 Reply Last reply
                                0
                                • jsulmJ jsulm

                                  @vinaygopal Can you show your current pro file?
                                  Also did you a clean rebuild?

                                  V Offline
                                  V Offline
                                  vinaygopal
                                  wrote on last edited by
                                  #21

                                  @jsulm yes i clean the build run qmake and then build the file everytime i change the pro file

                                  #-------------------------------------------------
                                  #
                                  # Project created by QtCreator 2019-11-21T10:31:03
                                  #
                                  #-------------------------------------------------
                                  
                                  QT       += core gui networkauth
                                  
                                  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                                  
                                  TARGET = example_project
                                  TEMPLATE = app
                                  
                                  
                                  # The following define makes your compiler emit warnings if you use
                                  # any feature of Qt which has been marked as deprecated (the exact warnings
                                  # depend on your compiler). Please consult the documentation of the
                                  # deprecated API in order to know how to port your code away from it.
                                  DEFINES += QT_DEPRECATED_WARNINGS
                                  
                                  # You can also make your code fail to compile if you use deprecated APIs.
                                  # In order to do so, uncomment the following line.
                                  # You can also select to disable deprecated APIs only up to a certain version of Qt.
                                  #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                                  
                                  CONFIG += c++11
                                  
                                  SOURCES += \
                                          main.cpp \
                                          google_widget.cpp
                                  
                                  HEADERS += \
                                          google_widget.h
                                  
                                  FORMS += \
                                          google_widget.ui
                                  
                                  # Default rules for deployment.
                                  qnx: target.path = /tmp/$${TARGET}/bin
                                  else: unix:!android: target.path = /opt/$${TARGET}/bin
                                  !isEmpty(target.path): INSTALLS += target
                                  
                                  DISTFILES += \
                                      upload_to_cloud.py
                                  
                                  
                                  #CONFIG += no_lflags_merge
                                  
                                  INCLUDEPATH="C:/Users/vinay/AppData/Local/Programs/Python/Python38/include"
                                  LIBS += $$PWD/../../../AppData/Local/Programs/Python/Python38/libs/python38.lib
                                  
                                  jsulmJ 1 Reply Last reply
                                  0
                                  • JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by JonB
                                    #22

                                    I'm keeping an eye on this thread. As I said right at the start

                                    You can indeed go down @jsulm's path, but embedding is a lot more involved than just running a subprocess.
                                    But as phrased if all you want to do is run an external script with some command-line arguments [...], it really is simple to do with QProcess, should take one minute to write.

                                    I do hope/presume that (for both your sakes) for use case here all this embedding is indeed required and worth the effort! :)

                                    V 1 Reply Last reply
                                    1
                                    • V vinaygopal

                                      @jsulm yes i clean the build run qmake and then build the file everytime i change the pro file

                                      #-------------------------------------------------
                                      #
                                      # Project created by QtCreator 2019-11-21T10:31:03
                                      #
                                      #-------------------------------------------------
                                      
                                      QT       += core gui networkauth
                                      
                                      greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                                      
                                      TARGET = example_project
                                      TEMPLATE = app
                                      
                                      
                                      # The following define makes your compiler emit warnings if you use
                                      # any feature of Qt which has been marked as deprecated (the exact warnings
                                      # depend on your compiler). Please consult the documentation of the
                                      # deprecated API in order to know how to port your code away from it.
                                      DEFINES += QT_DEPRECATED_WARNINGS
                                      
                                      # You can also make your code fail to compile if you use deprecated APIs.
                                      # In order to do so, uncomment the following line.
                                      # You can also select to disable deprecated APIs only up to a certain version of Qt.
                                      #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                                      
                                      CONFIG += c++11
                                      
                                      SOURCES += \
                                              main.cpp \
                                              google_widget.cpp
                                      
                                      HEADERS += \
                                              google_widget.h
                                      
                                      FORMS += \
                                              google_widget.ui
                                      
                                      # Default rules for deployment.
                                      qnx: target.path = /tmp/$${TARGET}/bin
                                      else: unix:!android: target.path = /opt/$${TARGET}/bin
                                      !isEmpty(target.path): INSTALLS += target
                                      
                                      DISTFILES += \
                                          upload_to_cloud.py
                                      
                                      
                                      #CONFIG += no_lflags_merge
                                      
                                      INCLUDEPATH="C:/Users/vinay/AppData/Local/Programs/Python/Python38/include"
                                      LIBS += $$PWD/../../../AppData/Local/Programs/Python/Python38/libs/python38.lib
                                      
                                      jsulmJ Online
                                      jsulmJ Online
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #23

                                      @vinaygopal Looks correct to me. I would try this:

                                      • Delete build directory
                                      • Run qmake
                                      • build

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

                                      1 Reply Last reply
                                      0
                                      • V vinaygopal

                                        When i click a button, i want to open up the python script (.py file) and run the python script written by me and exit python. I should be able to pass arguments to the python file through Qt c++? please help me out on how to achieve this. i did do some reading about QProcess class but i am not able to achieve this.

                                        J.HilkJ Offline
                                        J.HilkJ Offline
                                        J.Hilk
                                        Moderators
                                        wrote on last edited by J.Hilk
                                        #24

                                        @vinaygopal how dead set are you on Python as the scripting language?

                                        If it's not dictated and just a random selection, I'll want to offer 2 alternatives

                                        https://doc.qt.io/qt-5/qscriptengine.html#details
                                        and
                                        https://doc.qt.io/qt-5/qjsengine.html#details

                                        for QScript and JavaScript respectively, no external libs required


                                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                        Q: What's that?
                                        A: It's blue light.
                                        Q: What does it do?
                                        A: It turns blue.

                                        1 Reply Last reply
                                        0
                                        • JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by JonB
                                          #25

                                          @vinaygopal said in embed a python function into my Qt App.:
                                          @vinaygopal originally wrote just:

                                          i want to open up the python script (.py file) and run the python script written by me and exit python. I should be able to pass arguments to the python file through Qt c++? please help me out on how to achieve this. i did do some reading about QProcess class but i am not able to achieve this.

                                          If that is indeed all he wants to do, he may not need any integrated scripting, he may just need to make his QProcess attempt work and we would be done....

                                          If he would say why/what the script is supposed to do which requires embedding, we would all know what is required....

                                          1 Reply Last reply
                                          0

                                          • Login

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