PDA

View Full Version : executing .py file from qt



prachi kamble
17th June 2015, 12:36
hi,

I am writing a Qt application, where i have a "helloworld.py" and "helloworld .ui" file.
but i m getting trouble here, i wanna run /execute this .py file from within the qt application.
i have searched a lot on internet, and came across the QProcess class.
i tried to use it.(used start and execute methods from the class), but i dint get the results,
can anyone pls ans my question.

my code is bellow



QProcess process;
QString scriptFile = "D://QTDev//QuickEditor//helloworld.py";
process.execute("Python D://QTDev//QuickEditor//helloworld.py");

process.start("cd D://QTDev//QuickEditor//");
process.start("Python D://QTDev//QuickEditor//helloworld.py");
process.waitForFinished(1000);




from PyQt5 import QtCore, QtGui, QtWidgets
import sys

class Ui_Form(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)

def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.verticalLayout = QtWidgets.QVBoxLayout(Form)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.label = QtWidgets.QLabel(Form)
self.label.setText("")
self.label.setObjectName("label")
self.verticalLayout.addWidget(self.label)

self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)

def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "Press Me!!!"))
self.pushButton.clicked.connect(self.printHello)

def printHello(self):
print( "Hello World !")


if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv);
ex = Ui_Form();
ex.show();
sys.exit(app.exec_())

anda_skoa
20th June 2015, 12:38
The first argument to QProcess::start() is the program you run, the second argument is a list of arguments for that program.

Cheers,
_

ChrisW67
21st June 2015, 12:27
First listing line 3: is python on the system PATH? Why are you doubling the forward slashes? This will be passed unchanged to the program: you likely need the native path separators

Line 5. This would not change the directory for the process at line 6 even if it worked.

prachi kamble
25th June 2015, 07:19
yes python is there on PATH..
what if i have no argument list.. still i have to give empty?

anda_skoa
25th June 2015, 10:07
yes python is there on PATH..
what if i have no argument list.. still i have to give empty?
No, there is an overload that does not require the arguments QStringList if you just want to run the Python interpreter without a script.

Cheers,
_

prachi kamble
25th June 2015, 13:05
thnx sir,
so it means the following code is correct?



QProcess process;

process.setWorkingDirectory("D:\\QTDev\\QuickEditor");
process.setProgram("python helloworld.py");
process.start("python helloworld.py");
process.waitForFinished(1000);

anda_skoa
25th June 2015, 14:16
No, unless you have an executable called "python hellowordl.py", which is highly unlikely, most programs don't have spaces in their program names.

Cheers,
_