简体   繁体   English

启动控制台应用程序,该应用程序使用来自QT Gui应用程序的环境变量

[英]Launch console application which uses environment variables from QT Gui application

I am currently making a GUI using QT4.8 which basically needs to launch a console application. 我目前正在使用QT4.8制作GUI,这基本上需要启动控制台应用程序。 However, because this console application tries to fetch some environment variables, I can't seem to manage to make this work. 但是,由于此控制台应用程序尝试获取一些环境变量,所以我似乎无法设法使它起作用。 I am using QProcess obviously and have tried several solutions : 我显然正在使用QProcess,并尝试了几种解决方案:

process->start("./yarpbridge", QStringList() << "--from" << "tmp.ini");

This solution does not spawn a console window and besides, by redirecting the output to qDebug() , it prints the erros corresponding to the lack of environment variables. 该解决方案不会产生控制台窗口,而且,通过将输出重定向到qDebug() ,它可以打印出缺少环境变量的错误信息。

process->start("gnome-terminal", QStringList() << "-e" << "zsh" << "-c" << "\"./yarpbridge --from tmp.ini"\");

This solution does launch a console window but it nevertheless displays the error messages because somehow .zshrc was probably not consulted when opening the console window. 此解决方案确实启动了控制台窗口,但是它仍然显示错误消息,因为打开控制台窗口时可能不参考.zshrc。

Would you have a solution that would allow me to do this, and even better that would not only work with "gnome-terminal" and "zsh" users ? 您是否有一个解决方案可以使我做到这一点,甚至更好的解决方案不仅适用于“ gnome-terminal”和“ zsh”用户?

Thanks a lot, 非常感谢,

Can you post the error you are getting? 您可以发布您收到的错误吗?

It is very strange because you don't need to start a terminal in order to run a CLI program, maybe after posting your error message I might get an idea what the problem is. 这很奇怪,因为您不需要启动终端即可运行CLI程序,也许在发布错误消息后,我可能会知道问题出在哪里。

Also you can try this as well: 您也可以尝试以下方法:

#include <stdio.h>

char buffer[1024];
FILE* fd = popen("/path/to/yarpbridge", "r");

if (fd == NULL) {
    // Error: do something
}

while(NULL != fgets(buffer, sizeof(buffer), fd)) {
    QString s(buffer);
    s = s.stripWhiteSpace();
    // s contains the output, pretty much as readAllStandardOutput() in QProcess
}

// don't forget to close file.
close (fd);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM