1

Everything I read indicates that QDesktopServices::openUrl is the platform-agnostic way to open a document using the default program, and it works great, almost all of the time.

I say almost, because it always seems to fail if I pass it a network path. Combing through the source I see that ShellExecute returns error code 2.

Example:

QUrl localpath = QUrl::fromLocalFile("C:/temp/myfile.txt");
QUrl networkpath = QUrl::fromLocalFile("//192.168.0.5/my folder/myfile.txt");

QDesktopServices::openUrl(localpath);     //works fine
QDesktopServices::openUrl(networkpath);   //always fails

Is there a way I can clean up a network path to open correctly? I'm running Qt 4.8 on Windows. Switching the forward slashes to backslashes doesn't help.

Phlucious
  • 3,704
  • 28
  • 61
  • First and foremost you should use `QUrl::fromLocalFile` to build `file://` URLs, and not mere string concatenation. Second, I think you need to map that network path to a unit drive in order to make your shell happy. (Or is Windows just go to transparently handle such paths? I strongly doubt so, but who knows...) – peppe Feb 02 '16 at 21:35
  • Think having five slashes in a row might have something to do with it. – user4581301 Feb 02 '16 at 21:42
  • @peppe: I've updated the example to include `QUrl::fromLocalFile` instead of QString, but it doesn't help. I'd be shocked if the shell doesn't like network paths since _everything_ else does, but I guess I shouldn't be. – Phlucious Feb 02 '16 at 21:55
  • I wonder if having a space in the name (e.g. "my folder") is a problem? You might try a path that doesn't contain a space, just to see if it makes any difference. – Jeremy Friesner Feb 02 '16 at 22:05
  • No change. I think the Windows shell is simply incapable of handling network paths. For example, MS-DOS requires that you map a network drive first. [QProcess::startDetached](http://doc.qt.io/qt-4.8/qprocess.html#startDetached-2) looks like my best option. – Phlucious Feb 03 '16 at 00:03

1 Answers1

1

You should use QUrl::TolerantMode if you have spaces in your path. try this:

QDesktopServices::openUrl(QUrl("file:////192.168.0.5/my folder/myfile.txt", QUrl::TolerantMode));
Chris
  • 11
  • 2