Execute a python script stored in qrc file
-
Good evening,
in my qt application I would like to run a python script through this code:void SandboxDialog::onExecPythonClicked(void) { QStringList params; params << ":/file/PythonSandboxClass.py"; moP.start("python3", params); moP.waitForFinished(); ui->moTextEdit->append("calling PythonSandboxClass.py"); }
the python file is placed in a python .qrc file
<RCC> <qresource prefix="/file"> <file>PythonSandboxClass.py</file> </qresource> </RCC>
but when I run the application, I get the following error:
/usr/bin/python3: can't open file '/home/Qt/DiagSendbox/bin/:/PythonSandboxClass.py': [Errno 2] No such file or directory
My questions are:
- in the qrc file can I load files with *.py extension
- if yes, where am I wrong?
-
Good evening,
in my qt application I would like to run a python script through this code:void SandboxDialog::onExecPythonClicked(void) { QStringList params; params << ":/file/PythonSandboxClass.py"; moP.start("python3", params); moP.waitForFinished(); ui->moTextEdit->append("calling PythonSandboxClass.py"); }
the python file is placed in a python .qrc file
<RCC> <qresource prefix="/file"> <file>PythonSandboxClass.py</file> </qresource> </RCC>
but when I run the application, I get the following error:
/usr/bin/python3: can't open file '/home/Qt/DiagSendbox/bin/:/PythonSandboxClass.py': [Errno 2] No such file or directory
My questions are:
- in the qrc file can I load files with *.py extension
- if yes, where am I wrong?
@Renio
Resources are placed physically within the executable file. Qt code can access them via:/...
syntax, but nothing outside can, they are not files in the operating system.To do what you want you would need to extract that file to an external
.py
file, getpython3
to execute that, and finally delete the file. -
@Renio
Resources are placed physically within the executable file. Qt code can access them via:/...
syntax, but nothing outside can, they are not files in the operating system.To do what you want you would need to extract that file to an external
.py
file, getpython3
to execute that, and finally delete the file.@JonB Thank you for your reply.
I do this because I don't want insert the path of python file into my code, or avoid that someone replace my script.
this is my pro fileQT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ SandboxDialog.cpp HEADERS += \ SandboxDialog.h FORMS += \ SandboxDialog.ui TRANSLATIONS += \ DiagSendbox_it_IT.ts CONFIG += lrelease # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target DISTFILES += \ PythonSandboxClass.py RESOURCES += \ python.qrc
I undestrud that i can't to do this, correct?
-
@JonB Thank you for your reply.
I do this because I don't want insert the path of python file into my code, or avoid that someone replace my script.
this is my pro fileQT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ SandboxDialog.cpp HEADERS += \ SandboxDialog.h FORMS += \ SandboxDialog.ui TRANSLATIONS += \ DiagSendbox_it_IT.ts CONFIG += lrelease # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target DISTFILES += \ PythonSandboxClass.py RESOURCES += \ python.qrc
I undestrud that i can't to do this, correct?
-
R Renio has marked this topic as solved on