QUrl Class

Header:    #include <QUrl>
qmake:     QT += core

注意:该类中的所有函数都是可重入的.
QUrl类为URLs操作提供了方便的接口.
它可以解析并且构造URLs在编码和解码格式.QUrl也支持国际域名(IDNs).
The most common way to use QUrl is to initialize it via the constructor by passing a QString. Otherwise, setUrl() can also be used.
使用QUrl最常用的方式就是通过构造器 .否则,setUrl()也可以被使用.
URLs can be represented in two forms: encoded or unencoded. The unencoded representation is suitable for showing to users, but the encoded representation is typically what you would send to a web server. For example, the unencoded URL "http://bühler.example.com/List of applicants.xml" would be sent to the server as "http://xn--bhler-kva.example.com/List%20of%20applicants.xml".

A URL can also be constructed piece by piece by calling setScheme(), setUserName(), setPassword(), setHost(), setPort(), setPath(), setQuery() and setFragment(). Some convenience functions are also available: setAuthority() sets the user name, password, host and port. setUserInfo() sets the user name and password at once.

Call isValid() to check if the URL is valid. This can be done at any point during the constructing of a URL. If isValid() returns false, you should clear() the URL before proceeding, or start over by parsing a new URL with setUrl().

Constructing a query is particularly convenient through the use of the QUrlQuery class and its methods QUrlQuery::setQueryItems(), QUrlQuery::addQueryItem() and QUrlQuery::removeQueryItem(). Use QUrlQuery::setQueryDelimiters() to customize the delimiters used for generating the query string.

For the convenience of generating encoded URL strings or query strings, there are two static functions called fromPercentEncoding() and toPercentEncoding() which deal with percent encoding and decoding of QStrings.

Calling isRelative() will tell whether or not the URL is relative. A relative URL can be resolved by passing it as argument to resolved(), which returns an absolute URL. isParentOf() is used for determining whether one URL is a parent of another.

fromLocalFile() constructs a QUrl by parsing a local file path. toLocalFile() converts a URL to a local file path.

The human readable representation of the URL is fetched with toString(). This representation is appropriate for displaying a URL to a user in unencoded form. The encoded form however, as returned by toEncoded(), is for internal use, passing to web servers, mail clients and so on. Both forms are technically correct and represent the same URL unambiguously -- in fact, passing either form to QUrl‘s constructor or to setUrl() will yield the same QUrl object.

QUrl conforms to the URI specification from RFC 3986 (Uniform Resource Identifier: Generic Syntax), and includes scheme extensions from RFC 1738 (Uniform Resource Locators). Case folding rules in QUrl conform to RFC 3491 (Nameprep: A Stringprep Profile for Internationalized Domain Names (IDN)). It is also compatible with the file URI specification from freedesktop.org, provided that the locale encodes file names using UTF-8 (required by IDN).

Error checking

QUrl is capable of detecting many errors in URLs while parsing it or when components of the URL are set with individual setter methods (like setScheme(), setHost() or setPath()). If the parsing or setter function is successful, any previously recorded error conditions will be discarded.

By default, QUrl setter methods operate in QUrl::TolerantMode, which means they accept some common mistakes and mis-representation of data. An alternate method of parsing is QUrl::StrictMode, which applies further checks. See QUrl::ParsingMode for a description of the difference of the parsing modes.

QUrl only checks for conformance with the URL specification. It does not try to verify that high-level protocol URLs are in the format they are expected to be by handlers elsewhere. For example, the following URIs are all considered valid by QUrl, even if they do not make sense when used:

"http:/filename.html"
"mailto://example.com"
When the parser encounters an error, it signals the event by making isValid() return false and toString() / toEncoded() return an empty string. If it is necessary to show the user the reason why the URL failed to parse, the error condition can be obtained from QUrl by calling errorString(). Note that this message is highly technical and may not make sense to end-users.

QUrl is capable of recording only one error condition. If more than one error is found, it is undefined which error is reported.

Character Conversions

Follow these rules to avoid erroneous character conversion when dealing with URLs and strings:

When creating an QString to contain a URL from a QByteArray or a char*, always use QString::fromUtf8().

时间: 2024-10-20 01:49:10

QUrl Class的相关文章

QT打开网页 QURL

用QT打开一个网页就是先定义一个QUrl对象url,然后利用QDesktopServices::open(url)即可. 例如: const QUrl url(http://www.baidu.com); qDebug() << url.scheme(); qDebug() << url.port(); QDesktopServices::openUrl(url);

Qt打开外部程序和文件夹需要注意的细节(注意QProcess的空格问题,以及打开本地文件时,需要QUrl::fromLocalFile才可以)

下午写程序中遇到几个小细节,需要在这里记录一下. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 QProcess *process = new QProcess(this);     QFileInfo fileinfo(appUrl);     QString appPath = QApplication::applicationDirPath()+SAVEDIR+"/"+fileinfo.fileName();     bool res = proce

Threads Events QObjects

Events and the event loop Being an event-driven toolkit, events and event delivery play a central role in Qt architecture. In this article we'll not give a comprehensive coverage about this topic; we'll instead focus on some thread-related key concep

将vim作为QT开发的IDE

转载请注明链接与作者huihui1988 用了一段时间的vim,喜欢上了这种简洁高效的编辑器.恰逢正在学习QT中,于是将vim变成了开发QT的工具.以下是具体配置. 一.语法高亮支持: 1.打开VIMDIR/vim72/syntax/cpp.vim,加入 syn keyword cppStatement SLOT,SIGNAL syn keyword cppAccess slots,signals 2.增加QT的类型名: syn keyword cType ActiveQt Q3Accel Q3

浏览器

#!/usr/bin/env python #-*- coding:utf-8 -*- from PyQt4 import QtGui, QtCore, QtWebKit class MyPage(QtWebKit.QWebPage): def __init__(self, parent=None): super(MyPage, self).__init__(parent) def triggerAction(self, action, checked=False): if action == 

第九课、Qt之高级网络操作(HTTP/FTP快速上手)

一. HTTP 的消息结构 1.Request (1).请求行:Request 消息中的第一行,由请求方式.请求URL.HTTP协议及版本三部分组成 (2).请求头:其中 Content-Type 指定了客户端发送的内容格式.例如:Content-Type: application/json,指客户端发送的内容格式为 Json (3).请求体:要发送的表单数据 2.Response (1).状态行:Response 消息中的第一行,由 HTTP 协议版本号.状态码.状态消息三部分组成.状态码用来

关于QT的系统总结(非常全面,非常好)

源地址:http://www.cnblogs.com/wangqiguo/p/4625611.html 阅读目录 编译环境与开发流程 QT项目的构成及原理 QT中的布局 QT中的通用控件 QVariant 类型 QComboBox控件 QTableWidget控件 QTabWidget控件 QWebview控件 使用QSS 编码问题 QT的内存管理 QT的信号槽 QT中绘图 QT的线程 QT中使用第三方的dll QT中为控件添加右键菜单的方法 结束语 回到顶部 编译环境与开发流程 开发QT有两种

Qt通过HTTP POST上传文件

本文使用Qt Creator用HTTP POST的方法上传文件,并给出一个上传文件的例程. 本文主要客户端,所以对于服务器端程序编写的描述会比较简略 服务器使用Django编写,django服务器接收文件的方法在文章http://www.cnblogs.com/fnng/p/3740274.html中有较为清晰的讲解,我搭建的服务器端程序除了没有网页客户端以及部分变量名称不同以外,基本上与这篇文章的服务器搭建过程一样. 如果服务器端程序发生变化,这篇文章后面给出的客户端例程可能就不再适用.因此如

qt cef嵌入web

原文http://blog.sina.com.cn/s/blog_9e59cf590102vnfc.html 最近项目需要,研究了下libcef库. Cef(Chromium Embedded Framework)简述 嵌入式Chromium框架(简称CEF) 是一个由Marshall Greenblatt在2008建立的开源项目,它主要目的是开发一个基于Google Chromium的Webbrowser控件.CEF支持一系列的编程语言和操作系统,并且能很容易地整合到新的或已有的工程中去. 它