embed a python function into my Qt App.
-
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.
-
@vinaygopal
Which is why I asked as I did at the start! Really wish you had said!So you simply want to pass some string filenames to a script which does something, right? No need for integrated scripting.
You need to tell us what actually happens/goes wrong!
First just make your Python script print out everything in
sys.argv
so we can see.Oh, I see, you haven't twigged that at all. You can't just write a function in a standalone Python script/program. You need to have a main program in your script, and access
argv
for the command-line arguments!import sys # Main entry point of app if __name__ == "__main__": # args = sys.argv # count = len(sys.argv) while i < len(sys.argv): arg = sys.argv[i] print(arg)
There are millions of examples on the web.
Separately, you'd better hope all your commands etc are happy with those
/
path separators rather than native\
. I don't want to debate this again with others: some Windows stuff is happy with/
, some is not. -
@vinaygopal Take a look at https://docs.python.org/2/extending/embedding.html No need for QProcess I think.
-
@vinaygopal
You can indeed go down @jsulm's path, but embedding is a lot more involved than just running a subprocess. Which one you need depends on what this script is trying to do/what you want to achieve.If you want to pass arguments in/receive arguments back then in-process Python embedding may indeed be best. But as phrased if all you want to do is run an external script with some command-line arguments just like any other OS command but it happens to be written in Python, it really is simple to do with
QProcess
, should take one minute to write.i did do some reading about QProcess class but i am not able to achieve this.
If you wish to pursue this route, what does this mean? Show some code, it's dead easy. For example, the simplest might be a one-liner using https://doc.qt.io/qt-5/qprocess.html#execute:
int result = QProcess::execute("python", QStringList() << arg1 << arg2);
-
@jsulm i have to include the python library files in order to include Python.h can you please tell me how to specify lib path in QT in order for me to achieve this?
i am following this tutorial (https://www.codeproject.com/Articles/820116/Embedding-Python-program-in-a-C-Cplusplus-code) now i can include the .h file but linking it with the library i am having some problems as i do not know how to specify the library path as per the tutorial or if you know anyother way this can be achieved please let me know.
-
@vinaygopal Please take a look at https://doc.qt.io/qt-5/qmake-variable-reference.html#libs
-
@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"
-
@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?
-
This post is deleted!
-
@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. */
^
-
@vinaygopal This is not linker error but compiler.
Try to include the header like this:extern "C" { #include <Python.h> }
-
@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.
-
@vinaygopal Can you show your pro file?
-
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
-
@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
-
@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?
-
@vinaygopal One question: do you build your app as 64bit? Your Python seems to be 32bit.
-
@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
-
@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.
-
@vinaygopal Can you show your current pro file?
Also did you a clean rebuild?