Results 1 to 7 of 7

Thread: executing .py file from qt

  1. #1
    Join Date
    Apr 2015
    Posts
    26
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded PyQt3 PyQt4
    Platforms
    Windows

    Default executing .py file from qt

    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

    Qt Code:
    1. QProcess process;
    2. QString scriptFile = "D://QTDev//QuickEditor//helloworld.py";
    3. process.execute("Python D://QTDev//QuickEditor//helloworld.py");
    4.  
    5. process.start("cd D://QTDev//QuickEditor//");
    6. process.start("Python D://QTDev//QuickEditor//helloworld.py");
    7. process.waitForFinished(1000);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. from PyQt5 import QtCore, QtGui, QtWidgets
    2. import sys
    3.  
    4. class Ui_Form(QtWidgets.QWidget):
    5. def __init__(self):
    6. QtWidgets.QWidget.__init__(self)
    7. self.setupUi(self)
    8.  
    9. def setupUi(self, Form):
    10. Form.setObjectName("Form")
    11. Form.resize(400, 300)
    12. self.verticalLayout = QtWidgets.QVBoxLayout(Form)
    13. self.verticalLayout.setObjectName("verticalLayout")
    14. self.pushButton = QtWidgets.QPushButton(Form)
    15. self.pushButton.setObjectName("pushButton")
    16. self.verticalLayout.addWidget(self.pushButton)
    17. self.label = QtWidgets.QLabel(Form)
    18. self.label.setText("")
    19. self.label.setObjectName("label")
    20. self.verticalLayout.addWidget(self.label)
    21.  
    22. self.retranslateUi(Form)
    23. QtCore.QMetaObject.connectSlotsByName(Form)
    24.  
    25. def retranslateUi(self, Form):
    26. _translate = QtCore.QCoreApplication.translate
    27. Form.setWindowTitle(_translate("Form", "Form"))
    28. self.pushButton.setText(_translate("Form", "Press Me!!!"))
    29. self.pushButton.clicked.connect(self.printHello)
    30.  
    31. def printHello(self):
    32. print( "Hello World !")
    33.  
    34.  
    35. if __name__ == '__main__':
    36. app = QtWidgets.QApplication(sys.argv);
    37. ex = Ui_Form();
    38. ex.show();
    39. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 20th June 2015 at 13:36. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: executing .py file from qt

    The first argument to QProcess::start() is the program you run, the second argument is a list of arguments for that program.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: executing .py file from qt

    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.

  4. #4
    Join Date
    Apr 2015
    Posts
    26
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded PyQt3 PyQt4
    Platforms
    Windows

    Default Re: executing .py file from qt

    yes python is there on PATH..
    what if i have no argument list.. still i have to give empty?

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: executing .py file from qt

    Quote Originally Posted by prachi kamble View Post
    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,
    _

  6. #6
    Join Date
    Apr 2015
    Posts
    26
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded PyQt3 PyQt4
    Platforms
    Windows

    Default Re: executing .py file from qt

    thnx sir,
    so it means the following code is correct?

    Qt Code:
    1. QProcess process;
    2.  
    3. process.setWorkingDirectory("D:\\QTDev\\QuickEditor");
    4. process.setProgram("python helloworld.py");
    5. process.start("python helloworld.py");
    6. process.waitForFinished(1000);
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: executing .py file from qt

    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,
    _

Similar Threads

  1. doubt in by executing script file in qt
    By iswaryasenthilkumar in forum Newbie
    Replies: 5
    Last Post: 20th June 2015, 12:44
  2. Error while executing the exe release file
    By harmodrew in forum Newbie
    Replies: 7
    Last Post: 21st September 2010, 11:21
  3. Problems with executing the *.exe file
    By iksarp in forum Newbie
    Replies: 14
    Last Post: 28th February 2010, 06:49
  4. output while executing should be redirected to text file
    By AnithaRagupathy in forum Qt Programming
    Replies: 3
    Last Post: 13th October 2007, 11:33
  5. Executing SQL scripts from a file
    By William Wilson in forum Qt Programming
    Replies: 2
    Last Post: 30th August 2007, 20:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.