如何在Mac OS X中使用QProcess的正确方法

时间:2019-02-04 13:46:52

标签: c++ qt

我正在使用QProcess来获取haproxy的路径。它不能在Macos上很好地工作,但是在Linux上可以完美地工作。

这是我的一部分代码:

QString haProxyPath = NULL;

// try to find haproxy correctly
QProcess shellProcess;
shellProcess.start("/bin/sh");
shellProcess.write("which haproxy");
shellProcess.closeWriteChannel();
shellProcess.waitForFinished(-1);
haProxyPath = shellProcess.readAllStandardOutput().trimmed();

// linux shows the right path
// Macosx just show ""
qDebug() << "HAProxy Path " << haProxyPath;


// ha proxy location not found if output from command is empty
if (haProxyPath.isEmpty()) {
    qDebug() << "HAProxy not found!";
    return false;
}

但是如果在Macosx终端上使用which haproxy,我可以获得正确的值。

我使用了QT文档作为参考:http://doc.qt.io/qt-5/qprocess.html

有什么主意吗?

我找到了解决问题的一种方法,但是我认为这不是最好的方法。 我正在检查是否有haproxy文件:

#if defined(Q_OS_MAC)
    // verify if thw haproxy exist in Mac if not send an alert
    QFileInfo check_haproxy_exist_osx("/usr/local/bin/haproxy");
    if(check_haproxy_exist_osx.exists()){
        haProxyPath = "/usr/local/bin/haproxy";
    }
#endif

0 个答案:

没有答案
相关问题