Escape Spaces in QT

2019-08-09 00:52发布

问题:

I want to use the QString.split(' ') method to split a input command into the command

    QStringList commandList =  command.split(' ');

however command has a UNIX path at the end of it. i.e. it looks something like

    QString command = new QString("caommand -a -b /path/to\ specific/file");

the path command is specified by the user at runtime(the user escapes any spaces in the path). For some reason command.split(' '); does not escape the spaces.

I am new to QT, how does it escape spaces?

Thanks for any help

回答1:

You can use QDir::toNativeSeparators() to convert it to unix style. And split received result by spaces, though you have got to figure out where are the spaces between commands and where are the possible spaces in filename

For example:

QString myUnixPath = QDir::toNativeSeparators("/home/path with spaces/");

will return unix style path, while

QString qtPath = QDir::fromNativeSeparators("/path/with\ spaces/");

will return /path with spaces/