#结论
我进行了一些研究,目前的结论是:荒谬。
QUrl::fromPercentEncoding()
是正确的方法,OP在UPDATE部分所做的应该成为标题中问题的接受答案。
我认为Qt的文档QUrl::toDisplayString
有点误导:
"返回URL的人类可读字符串表示形式。输出可以通过传递带选项的标志进行自定义。选项RemovePassword始终启用,因为密码不应向用户显示。
实际上它并没有声称具有任何解码能力,这里的文档对它的行为不清楚。但至少密码部分是正确的。我在Gitorious上找到了一些线索:
"添加QUrl ::toDisplayString(),它是没有密码的toString()。并修复toString()的文档,其中说这是用于向人类显示的方法,而这从来没有成立过。"
#测试代码
为了区分不同函数的解码能力,以下代码已在Qt 5.2.1上进行了测试(尚未在Qt 5.3上测试!)
QString target(/*path*/);
QUrl url_path(target);
qDebug() << "[Original String]:" << target;
qDebug() << "--------------------------------------------------------------------";
qDebug() << "(QUrl::toEncoded) :" << url_path.toEncoded(QUrl::FullyEncoded);
qDebug() << "(QUrl::url) :" << url_path.url();
qDebug() << "(QUrl::toString) :" << url_path.toString();
qDebug() << "(QUrl::toDisplayString) :" << url_path.toDisplayString(QUrl::FullyDecoded);
qDebug() << "(QUrl::fromPercentEncoding):" << url_path.fromPercentEncoding(target.toUtf8());
P.S. QUrl::url
是 QUrl::toString
的同义词。
[情况1]:当目标路径为"%_%"
时(测试编码功能):
[Original String]: "%_%"
--------------------------------------------------------------------
(QUrl::toEncoded) : "%25_%25"
(QUrl::url) : "%25_%25"
(QUrl::toString) : "%25_%25"
(QUrl::toDisplayString) : "%25_%25"
(QUrl::fromPercentEncoding): "%_%"
[案例2]: 当目标路径="Meow !"
(测试编码功能)时:
[Original String]: "Meow !"
--------------------------------------------------------------------
(QUrl::toEncoded) : "Meow%20!"
(QUrl::url) : "Meow !"
(QUrl::toString) : "Meow !"
(QUrl::toDisplayString) : "Meow%20!" // "Meow !" when using QUrl::PrettyDecoded mode
(QUrl::fromPercentEncoding): "Meow !"
[案例3]: 当目标路径 = "Meow|!"
(测试编码功能):
[Original String]: "Meow|!"
--------------------------------------------------------------------
(QUrl::toEncoded) : "Meow%7C!"
(QUrl::url) : "Meow%7C!"
(QUrl::toString) : "Meow%7C!"
(QUrl::toDisplayString) : "Meow|!" // "Meow%7C!" when using QUrl::PrettyDecoded mode
(QUrl::fromPercentEncoding): "Meow|!"
[案例 4]: 当目标路径 = "http://test.com/query?q=++e:xyz/en"
(未进行百分号编码)时:
[Original String]: "http://test.com/query?q=++e:xyz/en"
--------------------------------------------------------------------
(QUrl::toEncoded) : "http://test.com/query?q=++e:xyz/en"
(QUrl::url) : "http://test.com/query?q=++e:xyz/en"
(QUrl::toString) : "http://test.com/query?q=++e:xyz/en"
(QUrl::toDisplayString) : "http://test.com/query?q=++e:xyz/en"
(QUrl::fromPercentEncoding): "http://test.com/query?q=++e:xyz/en"
[情况5]: 当目标路径 = "http://test.com/query?q=%2B%2Be%3Axyz%2Fen"
(%编码)时:
[Original String]: "http://test.com/query?q=%2B%2Be%3Axyz%2Fen"
--------------------------------------------------------------------
(QUrl::toEncoded) : "http://test.com/query?q=%2B%2Be%3Axyz%2Fen"
(QUrl::url) : "http://test.com/query?q=%2B%2Be%3Axyz%2Fen"
(QUrl::toString) : "http://test.com/query?q=%2B%2Be%3Axyz%2Fen"
(QUrl::toDisplayString) : "http://test.com/query?q=%2B%2Be%3Axyz%2Fen"
(QUrl::fromPercentEncoding): "http://test.com/query?q=++e:xyz/en"
P.S. 我也遇到了Ilya在评论中提到的错误:QUrl中加号"+"的百分比编码似乎无法正常工作
#摘要
QUrl::toDisplayString
的结果不太明确。正如文档所述,必须谨慎使用QUrl::FullyDecoded
模式。无论您得到什么类型的URL,请使用QUrl::toEncode
进行编码,并在必要时使用QUrl::fromPercentEncoding
进行显示。
至于OP中提到的QWebView
中百分比编码URL的故障,需要更多详细信息进行调试。可能是使用了不同的函数和不同的模式。
#有用的资源
- RFC 3986(QUrl符合该标准)
- 编码表
- Gitorious上qurl.cpp的源代码