QUrl from a string (porting KUrl constructor or KCmdLineArgs::url)

Kevin Kofler kevin.kofler at chello.at
Fri Oct 17 23:44:19 BST 2014


Hi,

even after reading the porting guide and listening to David Faure's Akademy 
talk, I am still unsure about what the best way is to get a URL from a 
string, supporting:
* absolute URL (with explicit scheme:// only)
* absolute local file path
* relative path: everything neither an absolute URL nor an absolute local
  file path is assumed to be a relative path (relative to the local CWD)
as both KUrl::KUrl(QString) and KCmdLineArgs::url did (in slightly 
different, but similar ways).

This is needed, e.g., when processing command-line arguments.

I see the following APIs available:
* QUrl::QUrl(QString): accepts an absolute URL or a relative URL, but the
                       documentation explicitly warns that it must not be
                       used on absolute local file paths
* QUrl::fromLocalFile: accepts an absolute local file path or a relative
                       local file path, but does not accept a URL
* QUrl::fromUserInput: seems closest to what I want, but assumes a web
                       browser context and in particular assumes http:// in
                       situations that should really be treated as a
                       relative path in most contexts

So, is there any function anywhere that does what I want? Or do I have to 
write something like:
QDir::isAbsolutePath(str) ? QUrl::fromLocalFile(str) : QUrl(str)
? (And is QUrl(str) even OK for relative file paths or is there a difference 
between a relative URL and a relative local file path that I'm missing?)

        Kevin Kofler





More information about the kde-core-devel mailing list