QT执行shell脚本或者执行linux指令


由于我在做linux下的QT开发,有时候会用到shell脚本的辅助,但是需要QT运行shell脚本并获取执行结果,今天给大家分享下我的技巧,废话少说直接上代码:

//执行shell指令或者shell脚本的方法
QString Common::executeLinuxCmd(QString strCmd)
{
    QProcess p;
    p.start("bash", QStringList() <<"-c" << strCmd);
    p.waitForFinished();
    QString strResult = p.readAllStandardOutput();
    return strResult;
}

//实例
QString strResult1 = executeLinuxCmd("sudo sh /home/test.sh");

QString strResult2 = executeLinuxCmd("cat /etc/hostname");


大家根据自己的业务需求去处理strResult1和strResult2,相信我的代码很清晰了吧!!!

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM