1

I wrote two Qt applications. One is the main and the other is a side way.

I am running Linux. I read about QProcess so I wrote this code:

 QApplication a(argc, argv);
    MainWindow w;

    w.show();
    QProcess P(&w);
    QString programPath;
    programPath=
    "/Documents/Qt/test1-build-desktop-Qt_4_8_1_in_PATH__System__Release/test1";
    P.start(programPath);

    return a.exec();

However, nothing happens and just my main app (w) runs.

What is my fault? Please help me.

OhBeWise
  • 5,350
  • 3
  • 32
  • 60
Erfan Tavakoli
  • 336
  • 1
  • 6
  • 14
  • 3
    Connect signals `void error ( QProcess::ProcessError error ); void finished ( int exitCode, QProcess::ExitStatus ex); void started (); void stateChanged ( QProcess::ProcessState newState );` to see what happened – kassak Dec 26 '12 at 09:10
  • @kassak That should be an actual answer :) – Nikos C. Dec 26 '12 at 10:26
  • QObject::connect(&P,SIGNAL(error(QProcess::ProcessError)),); you mean this? what should i write for third parameter ? i mean how to see this signals result? – Erfan Tavakoli Dec 26 '12 at 12:05

3 Answers3

3

the issue is that P.start(programPath); is a non blocking operation. Furthermore, the application output is redirected , and can be accessible from the Qprocess object only.

Edit:

It seems that the path to the executable is incorrect. Anything which starts with "/" will be considered an absolute path.

You probably need to write a QObject subclass to monitor the process you started. This object will catch process signals as kassak pointed out.

class ProcessMonitor : public QObject {
   Q_OBJECT

public slots:

void notifyStart();

void handleError( QProcess::ProcessError error );

void notifyStop(int exitCode, QProcess::ExitStatus ex);
}

In each slot you can just print a message. Then you can do the connections

ProcessMonitor montinor;
QObject::connect(&P,SIGNAL(error(QProcess::ProcessError)), 
      &monitor,SLOT(handleError( QProcess::ProcessError error )) );
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • both my app are Gui , and i want to run both of them geraphicaly. i mean when second app run fist app steal run. how can i do that. – Erfan Tavakoli Dec 26 '12 at 12:17
1

My fault was in the path to executable.

I edit it, very simple and got it work.

QApplication a(argc, argv);
MainWindow w;
w.show();
QProcess P(&w);
QString programPath;
programPath=
    "/home/erfan/Documents/Qt/test1-build-desktop- Qt_4_8_1_in_PATH__System__Release/test1";
P.start(programPath);
return a.exec();

And it work properly.

Another way is to put the executable directly in root:

(/ somthings)

Maddy26
  • 107
  • 1
  • 10
Erfan Tavakoli
  • 336
  • 1
  • 6
  • 14
-1

You can use

#include <cstdlib>
std::system("/path/to/executable &");
tomxey
  • 92
  • 1
  • 6
  • That won't fix the OP's problem. If QProcess wasn't able to execute the binary, std::system() won't be able to either. Furthermore, your code is less portable (which systems recognize `&`?) – Nikos C. Dec 26 '12 at 10:24