I tried to construct QUrl
piece by piece:
QUrl url{"https://host.org/path"};
url.setScheme("http");
url.setPort(81);
url.setUserName("user");
url.setPassword("password");
url.setHost("server.com");
QUrlQuery urlQuery;
urlQuery.setQueryItems({{"key1", "value1"}, {"key2", "value2"}, {"key3", "value3"}});
url.setQuery(urlQuery);
url.setFragment("fragment");
//url.setPath("dir/file.htm");
qDebug() << url;
Output (password is accidentally missed on the way):
QUrl("http://user@server.com:81/path?key1=value1&key2=value2&key3=value3#fragment")
First of all, if QUrl
is default-constructed, then using setters I can't add anything into it at all.
In above code if I uncomment last but one line, then output became QUrl("")
. That is QUrl::setPath
clean up the whole internal representation of QUrl
instance.
Are both mentioned behaviours normal? Or are they the bugs?
I use Qt 5.7.1.
It seems, that simple string concatenation is much less bug prone.
To answer at least some of your questions:
qDebug() << url;
eats the password and that is a good thing. Why? Because qDebug and friends are often used to write log files and having password in log files or even on the console is bad, really bad. So the default is that qDebug eats the password. If you need it call qDebug() << url.toString()
. You have been warned ;)
Why QUrl url("server.com"); url.setScheme("http");
results in http:server.com
is because in QUrl url("server.com");
"server.com" is parsed and recognized as the path and not the host.
I am using 5.7.0 and using a default constructed QUrl with setters work fine:
QUrl url;
url.setScheme("http");
url.setHost("server.com");
qDebug() << url; // QUrl("http://server.com")
The reason why setPath
makes the URL null is because it is ill-formed. The path must start with an slash. Use url.setPath("/dir/file.htm");
.
Cheers and keep fighting!