Solved QDesktopServices::openUrl fails to open on Windows Platform.
-
Folks,
We have a function to build a file path to a local HTML file and invoke QDesktopServices::openUrl to open it. It works fine on the RHEL 8 platform, but fails on the Windows 10 platform (build in a MinGW64 environment). This is using Qt 5.15.
My understanding from the documentation that the only way to fail is to not launch the browser, but how can we determine why it fails beyond the simple Yes/No failure message we get from the Boolean.
Thanks,
Dale Pennington -
Hi,
Based on the Qt 5.15.2 backend code, you should have a warning printed on the command line in case of problems.
-
When we do a run, we get the following error :
-- Error: ShellExecute 'c:%5CCrossCheck-OSC-mod2%5C%5Cshare%5Cdoc%5Cxpatch-4.17.1%5Carchitecture%5Cmodel_tools%5Ccrosscheck%5CCrossCheckTOC.html' failed (error 2).
Given that %05C is the backslash character ('') that Windows uses as delimeters, and that a file exists at that path, I am not sure what to look at next.
-
@DalePennington said in QDesktopServices::openUrl fails to open on Windows Platform.:
We have a function to build a file path to a local HTML file and invoke QDesktopServices::openUrl to open it.
What is the path you built, as passed to
openUrl()
? -
The code chunk in question is :
// Replace all forward slashes with QDir::separator (in case we ever go to Windows) QString delimitedDocsPath = docsPath.replace("/", QDir::separator()); if (QFile::exists(delimitedDocsPath)) { if (!QDesktopServices::openUrl(QUrl(docsPath))) { std::string msg = "Error occured while loading "; msg += docsPath.toStdString(); msg += " in default browser.\n"; _crossCheckMessageDialogImpl->AppendStatusMessage(msg); } }
So what we print in the error dialog when we have the problem is :
Error occured while loading C:\CrossCheck-OSC-2\\share\doc\xpatch-4.17.1\architecture\model_tools\crosscheck\CrossCheckTOC.html in default browser.
So what we print is the path before we replaced the Dir seperator with windows seperator, the modified path passed an exists check before being passed to openUrl and then we got the in code error as noted just above, and the debug error as reported in my previous reply.
-
The double backslash looks fishy.
In any case, you should not need to do that conversion by hand. The only time you would need that is if you have to call an external library with such a path. Otherwise, as long as you use Qt, use forward slashes everywhere. Qt will do the conversion when needed.
You should also use QUrl::fromLocalFile to build your URL.
-
@SGaist
Went with your suggested implementation changes, and it all appears to work now.Thanks