#结论
我做了一些研究,到目前为止的结论是:荒谬的。
QUrl::fromPercentEncoding()
是要走的路,OP在UPDATE部分所做的应该是标题中问题的公认答案。
我认为 Qt 的文档QUrl::toDisplayString
有点误导:
“返回 URL 的人类可显示的字符串表示形式。可以通过传递带有选项的标志来自定义输出。始终启用选项 RemovePassword,因为永远不应向用户显示密码。”
实际上它没有声称任何解码能力,这里的文档不清楚它的行为。但至少密码部分是正确的。我在Gitorious 上找到了一些线索:
“添加 QUrl::toDisplayString(),它是没有密码的 toString()。并修复了 toString() 的文档,该文档说这是用于向人类显示的方法,而这从来都不是真的。”
#Test Code 为了辨别不同函数的解码能力。以下代码已在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());
PSQUrl::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"
PS 我还遇到了 Ilya 在评论中提到的错误:百分比编码似乎不适用于 QUrl 中的 '+'
#概括
结果QUrl::toDisplayString
是模棱两可的。正如文件所说,QUrl::FullyDecoded
必须小心使用该模式。无论您获得哪种类型的 URL,都可以对它们进行编码并在必要时QUrl::toEncode
显示它们。QUrl::fromPercentEncoding
至于QWebView
OP中提到的百分比编码URL的故障,需要更多的细节来调试它。不同的功能和不同的使用模式可能是原因。
#有用的资源
- RFC 3986(符合 QUrl)
- 编码表
- Gitorious 上 qurl.cpp 的来源