关于void QProcess::start参数问题的解决

函数原型:
void QProcess::start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite)

问题:
const QStringList &arguments 只能传入命令本身的参数,不能传入引用参数。

解决:
在windows下面需要使用setNativeArguments() 添加引用参数

Qt官方文档描述:
void QProcess::start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite)

Starts the given program in a new process, passing the command line arguments in arguments.

The QProcess object will immediately enter the Starting state. If the process starts successfully, QProcess will emit started(); otherwise, errorOccurred() will be emitted.

Note: Processes are started asynchronously, which means the started() and errorOccurred() signals may be delayed. Call waitForStarted() to make sure the process has started (or has failed to start) and those signals have been emitted.

Note: No further splitting of the arguments is performed.

Windows: The arguments are quoted and joined into a command line that is compatible with the CommandLineToArgvW() Windows function. For programs that have different command line quoting requirements, you need to use setNativeArguments(). One notable program that does not follow the CommandLineToArgvW() rules is cmd.exe and, by consequence, all batch scripts.

The OpenMode is set to mode.

If the QProcess object is already running a process, a warning may be printed at the console, and the existing process will continue running unaffected.

使用代码实例:

void Unpack()
{
    QProcess pExe(0);
    QString str7z;
    //命令exe
    str7z.append("E:\\maozg tools\\OuterNetPacketMaker\\Bin\\7z.exe");

    //命令参数
    QStringList strCMD;
    strCMD.append("x");
    strCMD.append("D:\\111.zip");

    //引用参数
    QString strArg;
    strArg.append("-y");
    strArg.append("-o");
    strArg.append("D:\\");
    strArg.append("*.txt");
    strArg.append("-r");

    pExe.setNativeArguments(strArg);

    pExe.start(str7z, strCMD);
    pExe.waitForStarted();
    pExe.waitForFinished();
    QString strTemp = QString::fromLocal8Bit(pExe.readAllStandardOutput());

    QMessageBox testMassage1;
    testMassage1.setText(strTemp);
    testMassage1.exec();
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,452评论 0 10
  • 在去年的某一天,花了四个小时把杜拉斯的《情人》看了一遍。这位法国女作家用悲怯的笔调描写了她与一位中国男子沉...
    艺宸之阅读 949评论 1 2
  • 在我们相识那一刻,时间都静止了。曾经以为某个人的出现会惊艳了时光都是故事小说里的情节。直到遇见你…… ...
    是布白吖阅读 399评论 0 2
  • 事虽小勿擅为苟擅为子道亏物虽小勿私藏苟私藏亲心伤译文:纵然是小事,也不要任性,擅自作主,而不向父母禀告。如果任性而...
    海天大路阅读 2,083评论 11 8
  • 反正 爱了 晚安的时候感情特别充沛 恨不能就在身边 好好抱一番 说些傻话不舍得睡去
    ninvxv阅读 214评论 0 0