Tracyliving
Tracyliving

Reputation: 21

QProcess doesn't work with "~" symbol

I want to use QProcess to launch an executable file, assuming it is located in the home directory.

The following code works:

    QProcess process;
    QString cmd = qgetenv("HOME") + "/executableFile";
    process.start(cmd);

However, it will not work if I use "~" to stand for home directory:

    QString cmd = "~/executableFile";

Maybe it is a silly question, but I really want to know why. Hope to get your help. Thanks!

Upvotes: 1

Views: 133

Answers (1)

jbm
jbm

Reputation: 3163

~ is a special character handled by the shell.

$ echo ~
/home/jbm

But when you use a QProcess, there's no such thing as a shell to launch your process (just an environment, which is not the same), hence the "~" is not handled, but rather seen as a character literal that would actually be part of the path to your executable.

Upvotes: 4

Related Questions