4

I try to launch internet explorer, So I use the below code

QProcess * process=new QProcess(this);
QString temp="C:\\Program Files\\Internet\ Explorer\\iexplore.exe";
process->startDetached(temp.toStdString().c_str());

But it doesn't work.

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
prabhakaran
  • 5,126
  • 17
  • 71
  • 107

2 Answers2

8

Try:

QProcess * process=new QProcess(this);
QString temp="\"C:\\Program Files\\Internet Explorer\\iexplore.exe\"";
process->startDetached(temp);

You need to use escaped quotes since the path has a space in it, or possibly escape all the spaces (you missed Program\ Files in the code you posted).

Adam W
  • 3,648
  • 22
  • 18
  • 2
    Even easier than escaping: use startDetached(temp, QStringList()). That one will do the escaping itself. I would always prefer the variant of QProcess::start/startDetached etc. that takes the args as QStringList, to avoid quoting issues. – Frank Osterfeld Nov 15 '10 at 15:42
  • 1
    @Frank: almost, but the problem is that the program name has spaces not the arguments, so you still need to quote or escape the path. – Adam W Nov 15 '10 at 17:07
  • No, you don't. If you use the QStringList() overload, there is no quoting necessary. QProcess::startDetached(QLatin1String("/path/to/Foo Bar"), QStringList()) works, while QProcess::startDetached(QLatin1String("/path/to/Foo Bar")) fails. There is no reason in the former case to interpret "Bar" as arguments, as the arguments are passed separately. Qt does the Right Thing and builds the correct commandline internally. – Frank Osterfeld Nov 15 '10 at 20:23
  • @Frank: Hm, never actually had to use this before, good to know. – Adam W Nov 16 '10 at 00:55
1

How about that?

QDir dir("C:\\");
QProcess::execute("explorer.exe", QStringList() << dir.toNativeSeparators(dir.path()));
mosg
  • 12,041
  • 12
  • 65
  • 87