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

时间:2022-07-22
本文章向大家介绍关于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();
}