Run another executable in my Qt app

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 08:42:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!