7

I am trying to write a GUI wrapper for one of my command line tools written in Python.
It was suggested to me that I should use Qt.

Below is my project's .cpp file:

#include "v_1.h"
#include "ui_v_1.h"
#include<QtCore/QFile>
#include<QtCore/QTextStream>
#include <QProcess>
#include <QPushButton>
v_1::v_1(QWidget *parent) :
    QMainWindow(parent),ui(new Ui::v_1)
    {
        ui->setupUi(this);
    }
    v_1::~v_1()
    {
        delete ui;
    }

void v_1::on_pushButton_clicked()
{
    QProcess p;
    p.start("python script -arg1 arg1");
    p.waitForFinished(-1);
    QString p_stdout = p.readAllStandardOutput();
    ui->lineEdit->setText(p_stdout);
}

Below is my project's header file:

#ifndef V_1_H
#define V_1_H
#include <QMainWindow>
namespace Ui {
class v_1;
}

class v_1 : public QMainWindow
{
    Q_OBJECT   
public:
    explicit v_1(QWidget *parent = 0);
    ~v_1();

private slots:
    void on_pushButton_clicked();
private:
    Ui::v_1 *ui;
};

#endif // V_1_H

The UI file is just a Push Button and a LineEdit widget.

I allocated the Push Button a slot when it is clicked. The on_pushButton_clicked() method works fine when I call some utilities like ls or ps, and it pipes the output of those commands to the LineEdit widget, but when I try calling a Python script, it does not show me anything on the LineEdit widget.

Any help would be greatly appreciated.

Philip Conrad
  • 1,451
  • 1
  • 13
  • 22
devoidfeast
  • 829
  • 3
  • 12
  • 22
  • I would suggest using PyQt instead, will be easier that way. – paulm Feb 28 '13 at 08:52
  • QPRocess has returns values for waitForStarted, waitForFinished(), you can read the stderr output (analogous to how you read stdout), there's QProcess::errorString(). Check those first to see what's going wrong. – Frank Osterfeld Feb 28 '13 at 10:53

3 Answers3

2

Have you tried the following:

  1. Make sure python is in your system path
  2. Pass parameters as stated in the documentation as a QStringList
  3. Change readAllStandardOutput to readAll while testing

void v_1::on_pushButton_clicked() 
{
    QProcess p;
    QStringList params;

    params << "script.py -arg1 arg1";
    p.start("python", params);
    p.waitForFinished(-1);
    QString p_stdout = p.readAll();
    ui->lineEdit->setText(p_stdout);
}
zeFree
  • 2,129
  • 2
  • 31
  • 39
mAsT3RpEE
  • 1,818
  • 1
  • 17
  • 14
1

Hunor's answer worked for me too. But I didn't use process ID. I did:

void MainWindow::on_pushButton_clicked()
{
   QString path = '/Somepath/mypath';
   QString  command("python");
   QStringList params = QStringList() << "script.py";

   QProcess *process = new QProcess();
   process->startDetached(command, params, path);
   process->waitForFinished();
   process->close();
}
Hailey Yoon
  • 477
  • 4
  • 5
-1

For me the below code worked:

void MainWindow::on_pushButton_clicked()
{
    QString path = QCoreApplication::applicationDirPath();
    QString  command("python");
    QStringList params = QStringList() << "script.py";

    QProcess *process = new QProcess();
    process->startDetached(command, params, path, &processID);
    process->waitForFinished();
    process->close();
}

path: you can set your own path
command: in which program you want to run (in this case python)
params: the script you want to be executed
&processID is for kill the process if the main window is closed