我正在使用 qt creator 在 linux 上编写一个小应用程序。 当我启动我的应用程序时,我希望它执行一个 shell 命令。我像这样使用 QProcess:
int main(int argc, char *argv[])
{
MyApplication a(argc, argv);
QProcess mapProc(&a);
QString command;
QStringList args;
command = "java";
args << "-jar" << "/home/$USER/MapServer/map.jar" << "localhost" << "9797" << "12123";
mapProc.start(command, args);
bool flag = mapProc.waitForStarted();
QProcess::ProcessState state = mapProc.state();
qDebug() << mapProc.errorString();
qDebug() << mapProc.pid();
/*/////////////////
some code
/////////////////*/
return a.exec();
}
但是当我的应用程序启动时,进程“mapProc”变成了僵尸。为什么?我做错了什么?
最佳答案
$USER
不会真正像 QProcess
那样工作。您将需要通过 /bin/sh -c "mycmd"
调用该命令,或者如果您只是按照如下所示的正确 Qt 方式来调用该命令,则效果会更好。
尝试使用 QStandardPaths ,所以这样写:
QString homeLocation =
QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
args << "-jar" << QString(homeLocation.first() + "/MapServer/map.jar")
<< "localhost" << "9797" << "12123";
而不是这个:
args << "-jar" << "/home/$USER/MapServer/map.jar"
<< "localhost" << "9797" << "12123";
关于c++ - 为什么起源进程在 Qt-app 中以僵尸启动。 Linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26711089/