Results 1 to 2 of 2

Thread: Running Python script on buttonclick in QT creator

  1. #1
    Join Date
    Aug 2017
    Location
    Philippines
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Running Python script on buttonclick in QT creator

    I am using qtcreator 4.0.1. I want to call python script in cmd prompt on a button click in qt creator. Using QProcess procedure is not resulting in any output. I have already many python scripts which needs to be incorporated into qt, so cannot go with using PythonQT. So I used cmd prompt to run scripts. Here is my code,but i am getting only the cmd prompt with no output.I have looked other ways of calling python scripts but didn't recieved expected results.

    Qt Code:
    1. void Databasewin::store_clicked()
    2. {
    3. QString cmd_qt = QString("C:/Python27/python p2.py");
    4.  
    5. const char* cmd1 = cmd_qt.toLocal8Bit().constData();
    6. system(cmd1);
    7. system("pause>nul");
    8. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Running Python script on buttonclick in QT creator

    You could try using QProcess instead:

    Qt Code:
    1. QString program( "C:/Python27/python.exe" );
    2. QStringList args = QStringList() << "py2.py";
    3. int exitCode = QProcess::execute( program, args );
    To copy to clipboard, switch view to plain text mode 

    If exitCode is -2, it means the program could not be started; if exitCode is -1 it means the program crashed. Otherwise, exitCode will be the exit value from python.exe when it exits.

    Also, be sure that your program and python's idea of the working directory is the same as yours. It is entirely possible that python is looking in C:/Python27 for the script, when in fact the script is somewhere else entirely. You could test this simply by replacing "py2.py" with "<absolute-path-to-scripts>/py2.py".
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Embedding PyQt4 into an Application running Python via Boost::Python
    By GreyHound in forum Installation and Deployment
    Replies: 1
    Last Post: 6th February 2012, 06:48
  2. error in Python script
    By HanyM.Magdy in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2010, 23:49
  3. System Error in Python embeded script
    By HanyM.Magdy in forum Qt Programming
    Replies: 0
    Last Post: 9th August 2010, 12:20
  4. Python Script in Qt C++
    By HanyM.Magdy in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2010, 16:47
  5. Replies: 7
    Last Post: 15th April 2007, 01:39

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.