Milad Kahsari Alhadi
Milad Kahsari Alhadi

Reputation: 513

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.

    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. 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. How can I do that?

Upvotes: 0

Views: 361

Answers (1)

code_fodder
code_fodder

Reputation: 16341

You could just use qstring split:

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

Upvotes: 1

Related Questions