Qt:QUrl
阅读原文时间:2023年07月11日阅读:2

概述

一个代表URL的类,此外还支持国际域名(IDNs)。

通常在初始化时传入QString构造QUrl,除此之外还能用setUrl()。

URL有两种表示格式:编码、未编码。未编码URL常用于显示,编码URL常用于发送给一个网络服务器进行处理,例如,未编码的URL"http://bühler.example.com/List of applicants.xml"发给服务器时就是"http://xn--bhler-kva.example.com/List%20of%20applicants.xml"。

分块构造URL是如何实现呢?可以通过setScheme()、setUserName()、setPassword()、setHost()、setPort()、setPath()、setQuery()、setFragment()分别构造一个URL每个分部。也可以使用一些综合的函数,比如setAuthority()可以同时设置Username、Password、Host、Port。setUserInfo()可以设置Username、Password。

如果要检查URL是否合法,可以调用isValid()方法。该方法可以用于构造一个URL的任何时期(这些时期就是上一段所说构造那些分块的时期)。如果isValid()返回false,那么在运行前就需要先调用clear()重置这个URL,之后通过setUrl()构造一个新的URL。

通过QUrlQuery类和它的相关方法可以构造一个Query。

调用static方法fromPercentEncoding()和toPercentEncoding()可以很方便的对URL和Query String进行编码和解码,返回值是QString类型。

fromLocalFile()方法通过解析一个本地文件构造一个QUrl,toLocalFile()则能将一个URL输出到一个本地文件中。

我们能够立刻辨识的URL可以通过toString()方法得到。这个方法可以很方便地用于展示一个非编码URL。如果想得到一个编码URL,可以用toEncoded(),编码URL常用于网络传输给Web服务器、邮箱客户端等等。URL不管编码与否都能无歧义且正确地代表同一个URL,也可以传入QUrl的构造方法或者setUrl()来产生一个相同的QUrl对象。

URL与Path

调用isRelative()可以判断URL是不是相对URL。相对URL没有scheme(暂时不知道咋翻译,就先用英文代替),一个scheme是指URL对应的Path中类似 xxx: 的前缀:

qDebug() << QUrl("main.qml").isRelative();          // true: no scheme  
qDebug() << QUrl("qml/main.qml").isRelative();      // true: no scheme  
qDebug() << QUrl("file:main.qml").isRelative();     // false: has "file" scheme  
qDebug() << QUrl("file:qml/main.qml").isRelative(); // false: has "file" scheme

不过要注意,URL的绝对/相对与Path的绝对/相对无关,Path的绝对相对是看Path是否从根目录开始;URL则是看是否有scheme:

// 相对URL, 绝对path  
url = QUrl("/home/user/file.txt");

// 绝对URL, 相对path  
QUrl url("file:file.txt");

一个相对URL可以通过resolved()转化为绝对URL。isParentOf()方法用于判断URL是否是另一个URL的Parent。

Header

#include

qmake

QT + = core

QUrl( QUrl  other )

QUrl( QString url , QUrl::ParsingMode = TolerantMode )

QUrl()

QString

fromAce(QByteArray domain)

 

QUrl

fromCFURL(CFURLRef url)

 

QUrl

fromEncoded(QByteArray input, QUrl::ParsingMode parsingMode = TolerantMode)

 

QUrl

fromLocalFile(QString localFile)

 

QUrl

fromNSURL(NSURL *_url_)

 

QString

fromPercentEncoding(QByteArray input)

 

QList

fromStringList(QStringList urls, QUrl::ParsingMode mode = TolerantMode)

 

QUrl

fromUserInput(QString userInput)

fromUserInput(QString userInput,QString workingDirectory, QUrl::UserInputResolutionOptions options = DefaultResolution)

 

QStringList

idnWhitelist()

 

void

setIdnWhitelist(QStringList list)

 

QByteArray

toAce(QString domain)

 

QByteArray

toPercentEncoding(QString input,QByteArray exclude = QByteArray(),QByteArray include = QByteArray())

 

QStringList

toStringList(QList urls, QUrl::FormattingOptions options = FormattingOptions(PrettyDecoded))

 

QURL &

operator=(QUrl other)

operator=(QString & url)

operator!=(QUrl url)

operator==(QUrl url)

 

QUrl

adjusted(QUrl::FormattingOptions options) const

 

QString

authority(QUrl::ComponentFormattingOptions options = PrettyDecoded)

 

void

clear()

 

QString

errorString()

 

QString

fileName(QUrl::ComponentFormattingOptions options = FullyDecoded)

 

QString

fragment(QUrl::ComponentFormattingOptions options = PrettyDecoded)

 

bool

hasFragment()

 

bool

hasQuery() 

 

QString

host(QUrl::ComponentFormattingOptions options = FullyDecoded)

 

bool

isEmpty()

 

bool

isLocalFile()

 

bool

isParentOf(QUrl childUrl)

 

bool

isRelative()

 

bool

isValid()

 

bool

matches(QUrl url, QUrl::FormattingOptions options)

 

QString

password(QUrl::ComponentFormattingOptions options = FullyDecoded)

 

QString

path(QUrl::ComponentFormattingOptions options = FullyDecoded)

 

int

port(int defaultPort = -1) 

 

QString

query(QUrl::ComponentFormattingOptions options = PrettyDecoded)

 

QUrl

resolved(QUrl relative)

 

QString

scheme()

 

void

setAuthority(QString authority, QUrl::ParsingMode mode = TolerantMode)

setFragment(QString fragment, QUrl::ParsingMode mode = TolerantMode)

setHost(QString host, QUrl::ParsingMode mode = DecodedMode)

setPassword(QString password, QUrl::ParsingMode mode = DecodedMode)

setPath(QString path, QUrl::ParsingMode mode = DecodedMode)

setPort(int port)

setQuery(QString query, QUrl::ParsingMode mode = TolerantMode)

setQuery(QUrlQuery query)

setScheme(QString scheme)

setUrl(QString url, QUrl::ParsingMode parsingMode = TolerantMode)

setUserInfo(QString userInfo, QUrl::ParsingMode mode = TolerantMode)

setUserName(QString userName, QUrl::ParsingMode mode = DecodedMode)

 

void

swap(QUrl &other)

 

CFURLRef

toCFURL()

 

QString

toDisplayString(QUrl::FormattingOptions options = FormattingOptions(PrettyDecoded))

 

QByteArray

toEncoded(QUrl::FormattingOptions options = FullyEncoded)

 

QString

toLocalFile()

 

NSURL *

toNSURL()

 

QString

toString(QUrl::FormattingOptions options = FormattingOptions(PrettyDecoded))

 

QString

url(QUrl::FormattingOptions options = FormattingOptions(PrettyDecoded)) const

 

QString

userInfo(QUrl::ComponentFormattingOptions options = PrettyDecoded)

 

QString

userName(QUrl::ComponentFormattingOptions options = FullyDecoded)

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章