问题
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.
回答1:
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 )) );
回答2:
My fault was in path to executable.
I edit it vary 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)
回答3:
You can use
#include <cstdlib>
std::system("/path/to/executable &");
来源:https://stackoverflow.com/questions/14037761/run-another-executable-in-my-qt-app