简体   繁体   English

使用 QProcess 执行命令并将结果存储在 QStringList 中

[英]Execute Command with QProcess and Store result in QStringList

I have written the following function which executes a windows enumeration command for getting shared folders and store the result in a QString.我编写了以下 function 执行 windows 枚举命令以获取共享文件夹并将结果存储在 QString 中。

    QProcess p;
    p.setProgram("wmic");
    p.setArguments({"share", "get", "name"});
    p.start();

    if (!p.waitForFinished()) {
        return;
    }

    const QString output = p.readAllStandardOutput();
    qDebug () << output;

    const QString error = p.readAllStandardError();
    if (!error.isEmpty()) {
        qDebug () << error;   
    }

But the output has a lot of delimiters like "\n\r"... so I wanted to strip all of those delimiters from my string output.但是 output 有很多分隔符,例如“\n\r”......所以我想从我的字符串 output 中删除所有这些分隔符。 In the next step, you consider we will have a result like the following one:在下一步中,您认为我们将得到如下结果:

C$
D$
E$
IPC$

So I wanted to save these names in a QStringList, or something like a list which I can append those names in combo widget independently.所以我想将这些名称保存在 QStringList 或类似列表中,我可以在组合小部件中独立地 append 这些名称。 How can I do that?我怎样才能做到这一点?

You could just use qstring split:您可以只使用 qstring 拆分:

QStringList list = output.split("\n", QString::SkipEmptyParts);

If you need a more "intelligent" split that you can pass in a regex:如果您需要更“智能”的拆分,您可以传入正则表达式:

list = output.split(QRegExp("...some regex..."));

The skip empty parts just "removes"/ignores any values that would be empty - I don't think you need that in this case跳过空白部分只是“删除”/忽略任何为空的值 - 我认为在这种情况下你不需要

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

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