0

Firstly, i have seen this answer.

I'm currently using Qt 5.8 on Mac, and tried below codes:

QString test("/Users/test/Documents/ASCII Tables.pdf");
qDebug() << test.replace(" ", "\\ ");

expected result shoud be like:

"/Users/test/Documents/ASCII\ Tables.pdf"

what i got is two blackslashes are added:

"/Users/test/Documents/ASCII\\ Tables.pdf"

Anyone has suggestion?


Update: (regarding to @docsteer's answer) i intended to use the QString test as part of QProcess:start(), for example:

QProcess process;
process.start("ls " + test);
process.waitForFinished(-1);
qDebug() << process.readAllStandardOutput();
qDebug() << process.readAllStandardError();

and the result is not as expected, as both backslashes are inserted into the command:

""
"ls: /Users/test/Documents/ASCII\\: No such file or directory\nls: Tables.pdf: No such file or directory\n"
Community
  • 1
  • 1
scmg
  • 1,904
  • 1
  • 15
  • 24

1 Answers1

3

What you are seeing there is not the actual content of the string. QDebug escapes the characters as it prints them:

http://doc.qt.io/qt-5/qdebug.html#operator-lt-lt-16

Normally, QDebug prints the string inside quotes and transforms non-printable characters to their Unicode values (\u1234).

To print non-printable characters without transformation, enable the noquote() functionality.

If you change your code to

qDebug().noquote() << test.replace(" ", "\\ ");

You will see what you expect - the raw content of the string

docsteer
  • 2,506
  • 16
  • 16
  • hi, thanks for the notice, but i updated the question, maybe another suggestion? ^_^ – scmg Apr 15 '17 at 22:58
  • OK, so that's a bit of an extension to the original question. I don't have a Mac handy to try this, but I suspect you need to use QProcess with arguments instead of putting it all in the command. Try: `QProcess process;QStringList arguments;arguments << test;process.setProgram("ls");process.setArguments(arguments);` – docsteer Apr 16 '17 at 08:31
  • oh yeah, it turns out when calling `process` with `program` and `arguments` we don't need to convert spaces to backslash spaces... thanks a lot – scmg Apr 16 '17 at 13:46