Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QUrl Class

TheQUrl class provides a convenient interface for working with URLs.More...

Header:#include <QUrl>

Note: All functions in this class arereentrant.

Public Types

enumFormattingOption { None, RemoveScheme, RemovePassword, RemoveUserInfo, ..., StripTrailingSlash }
flagsFormattingOptions
enumParsingMode { TolerantMode, StrictMode }

Public Functions

QUrl()
QUrl(const QString & url)
QUrl(const QUrl & other)
QUrl(const QString & url, ParsingMode parsingMode)
~QUrl()
voidaddEncodedQueryItem(const QByteArray & key, const QByteArray & value)
voidaddQueryItem(const QString & key, const QString & value)
QList<QByteArray>allEncodedQueryItemValues(const QByteArray & key) const
QStringListallQueryItemValues(const QString & key) const
QStringauthority() const
voidclear()
QByteArrayencodedFragment() const
QByteArrayencodedHost() const
QByteArrayencodedPassword() const
QByteArrayencodedPath() const
QByteArrayencodedQuery() const
QByteArrayencodedQueryItemValue(const QByteArray & key) const
QList<QPair<QByteArray, QByteArray> >encodedQueryItems() const
QByteArrayencodedUserName() const
QStringerrorString() const
QStringfragment() const
boolhasEncodedQueryItem(const QByteArray & key) const
boolhasFragment() const
boolhasQuery() const
boolhasQueryItem(const QString & key) const
QStringhost() const
boolisEmpty() const
boolisLocalFile() const
boolisParentOf(const QUrl & childUrl) const
boolisRelative() const
boolisValid() const
QStringpassword() const
QStringpath() const
intport() const
intport(int defaultPort) const
QStringqueryItemValue(const QString & key) const
QList<QPair<QString, QString> >queryItems() const
charqueryPairDelimiter() const
charqueryValueDelimiter() const
voidremoveAllEncodedQueryItems(const QByteArray & key)
voidremoveAllQueryItems(const QString & key)
voidremoveEncodedQueryItem(const QByteArray & key)
voidremoveQueryItem(const QString & key)
QUrlresolved(const QUrl & relative) const
QStringscheme() const
voidsetAuthority(const QString & authority)
voidsetEncodedFragment(const QByteArray & fragment)
voidsetEncodedHost(const QByteArray & host)
voidsetEncodedPassword(const QByteArray & password)
voidsetEncodedPath(const QByteArray & path)
voidsetEncodedQuery(const QByteArray & query)
voidsetEncodedQueryItems(const QList<QPair<QByteArray, QByteArray> > & query)
voidsetEncodedUrl(const QByteArray & encodedUrl)
voidsetEncodedUrl(const QByteArray & encodedUrl, ParsingMode parsingMode)
voidsetEncodedUserName(const QByteArray & userName)
voidsetFragment(const QString & fragment)
voidsetHost(const QString & host)
voidsetPassword(const QString & password)
voidsetPath(const QString & path)
voidsetPort(int port)
voidsetQueryDelimiters(char valueDelimiter, char pairDelimiter)
voidsetQueryItems(const QList<QPair<QString, QString> > & query)
voidsetScheme(const QString & scheme)
voidsetUrl(const QString & url)
voidsetUrl(const QString & url, ParsingMode parsingMode)
voidsetUserInfo(const QString & userInfo)
voidsetUserName(const QString & userName)
voidswap(QUrl & other)
QByteArraytoEncoded(FormattingOptions options = None) const
QStringtoLocalFile() const
QStringtoString(FormattingOptions options = None) const
QStringtopLevelDomain() const
QStringuserInfo() const
QStringuserName() const
booloperator!=(const QUrl & url) const
QUrl &operator=(const QUrl & url)
QUrl &operator=(const QString & url)
QUrl &operator=(QUrl && other)
booloperator==(const QUrl & url) const

Static Public Members

QStringfromAce(const QByteArray & domain)
QUrlfromEncoded(const QByteArray & input)
QUrlfromEncoded(const QByteArray & input, ParsingMode parsingMode)
QUrlfromLocalFile(const QString & localFile)
QStringfromPercentEncoding(const QByteArray & input)
QUrlfromUserInput(const QString & userInput)
QStringListidnWhitelist()
voidsetIdnWhitelist(const QStringList & list)
QByteArraytoAce(const QString & domain)
QByteArraytoPercentEncoding(const QString & input, const QByteArray & exclude = QByteArray(), const QByteArray & include = QByteArray())

Related Non-Members

uintqHash(const QUrl & url)
QDataStream &operator<<(QDataStream & out, const QUrl & url)
QDataStream &operator>>(QDataStream & in, QUrl & url)

Macros

Detailed Description

TheQUrl class provides a convenient interface for working with URLs.

It can parse and construct URLs in both encoded and unencoded form.QUrl also has support for internationalized domain names (IDNs).

The most common way to useQUrl is to initialize it via the constructor by passing aQString. Otherwise,setUrl() andsetEncodedUrl() can also be used.

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", and this can be verified by calling thetoEncoded() function.

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

CallisValid() to check if the URL is valid. This can be done at any point during the constructing of a URL.

Constructing a query is particularly convenient through the use ofsetQueryItems(),addQueryItem() andremoveQueryItem(). UsesetQueryDelimiters() 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 calledfromPercentEncoding() andtoPercentEncoding() which deal with percent encoding and decoding of QStrings.

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

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

The human readable representation of the URL is fetched withtoString(). This representation is appropriate for displaying a URL to a user in unencoded form. The encoded form however, as returned bytoEncoded(), is for internal use, passing to web servers, mail clients and so on.

QUrl conforms to the URI specification fromRFC 3986 (Uniform Resource Identifier: Generic Syntax), and includes scheme extensions fromRFC 1738 (Uniform Resource Locators). Case folding rules inQUrl conform toRFC 3491 (Nameprep: A Stringprep Profile for Internationalized Domain Names (IDN)).

Character Conversions

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

See alsoQUrlInfo.

Member Type Documentation

enum QUrl::FormattingOption
flags QUrl::FormattingOptions

The formatting options define how the URL is formatted when written out as text.

ConstantValueDescription
QUrl::None0x0The format of the URL is unchanged.
QUrl::RemoveScheme0x1The scheme is removed from the URL.
QUrl::RemovePassword0x2Any password in the URL is removed.
QUrl::RemoveUserInfoRemovePassword | 0x4Any user information in the URL is removed.
QUrl::RemovePort0x8Any specified port is removed from the URL.
QUrl::RemoveAuthorityRemoveUserInfo | RemovePort | 0x10 
QUrl::RemovePath0x20The URL's path is removed, leaving only the scheme, host address, and port (if present).
QUrl::RemoveQuery0x40The query part of the URL (following a '?' character) is removed.
QUrl::RemoveFragment0x80 
QUrl::StripTrailingSlash0x10000The trailing slash is removed if one is present.

Note that the case folding rules inNameprep, whichQUrl conforms to, require host names to always be converted to lower case, regardless of the Qt::FormattingOptions used.

The FormattingOptions type is a typedef forQFlags<FormattingOption>. It stores an OR combination of FormattingOption values.

enum QUrl::ParsingMode

The parsing mode controls the wayQUrl parses strings.

ConstantValueDescription
QUrl::TolerantMode0QUrl will try to correct some common errors in URLs. This mode is useful when processing URLs entered by users.
QUrl::StrictMode1Only valid URLs are accepted. This mode is useful for general URL validation.

In TolerantMode, the parser corrects the following invalid input:

  • Spaces and "%20": If an encoded URL contains a space, this will be replaced with "%20". If a decoded URL contains "%20", this will be replaced with a single space before the URL is parsed.
  • Single "%" characters: Any occurrences of a percent character "%" not followed by exactly two hexadecimal characters (e.g., "13% coverage.html") will be replaced by "%25".
  • Reserved and unreserved characters: An encoded URL should only contain a few characters as literals; all other characters should be percent-encoded. In TolerantMode, these characters will be automatically percent-encoded where they are not allowed: space / double-quote / "<" / ">" / "[" / "" / "]" / "^" / "`" / "{" / "|" / "}"

Member Function Documentation

QUrl::QUrl()

Constructs an emptyQUrl object.

QUrl::QUrl(constQString & url)

Constructs a URL by parsingurl.url is assumed to be in human readable representation, with no percent encoding.QUrl will automatically percent encode all characters that are not allowed in a URL. The default parsing mode isTolerantMode.

Example:

QUrl url("http://www.example.com/List of holidays.xml");// url.toEncoded() == "http://www.example.com/List%20of%20holidays.xml"

To construct a URL from an encoded string, callfromEncoded():

QUrl url=QUrl::fromEncoded("http://qt.nokia.com/List%20of%20holidays.xml");

See alsosetUrl(),setEncodedUrl(),fromEncoded(), andTolerantMode.

QUrl::QUrl(constQUrl & other)

Constructs a copy ofother.

QUrl::QUrl(constQString & url,ParsingMode parsingMode)

This is an overloaded function.

Parses theurl using the parser modeparsingMode. The default parsing mode isTolerantMode.

See alsosetUrl().

QUrl::~QUrl()

Destructor; called immediately before the object is deleted.

void QUrl::addEncodedQueryItem(constQByteArray & key, constQByteArray & value)

Inserts the pairkey =value into the query string of the URL.

Note: this function does not verify that eitherkey orvalue are properly encoded. It is the caller's responsibility to ensure that the query delimiters are properly encoded, if any.

This function was introduced in Qt 4.4.

See alsoaddQueryItem() andsetQueryDelimiters().

void QUrl::addQueryItem(constQString & key, constQString & value)

Inserts the pairkey =value into the query string of the URL.

The key/value pair is encoded before it is added to the query. The pair is converted into separate strings internally. Thekey andvalue is first encoded into UTF-8 and then delimited by the character returned by valueDelimiter(). Each key/value pair is delimited by the character returned by pairDelimiter().

Note:This method does not encode spaces (ASCII 0x20) as plus (+) signs, like HTML forms do. If you need that kind of encoding, you must encode the value yourself and useQUrl::addEncodedQueryItem.

See alsoaddEncodedQueryItem().

QList<QByteArray> QUrl::allEncodedQueryItemValues(constQByteArray & key) const

Returns the a list of query string values whose key is equal tokey from the URL.

Note: if the encodedkey does not match the encoded version of the query, this function will not work. That is, if the encoded query of this URL is "search=Qt%20Rules", calling this function withkey = "%73earch" will return an empty list.

This function was introduced in Qt 4.4.

See alsoallQueryItemValues(),queryItemValue(), andencodedQueryItemValue().

QStringList QUrl::allQueryItemValues(constQString & key) const

Returns the a list of query string values whose key is equal tokey from the URL.

Note:This method does not decode spaces plus (+) signs as spaces (ASCII 0x20), like HTML forms do. If you need that kind of decoding, you must useQUrl::allEncodedQueryItemValues and decode the data yourself.

See alsoqueryItemValue().

QString QUrl::authority() const

Returns the authority of the URL if it is defined; otherwise an empty string is returned.

See alsosetAuthority().

void QUrl::clear()

Resets the content of theQUrl. After calling this function, theQUrl is equal to one that has been constructed with the default empty constructor.

QByteArray QUrl::encodedFragment() const

Returns the fragment of the URL if it is defined; otherwise an empty string is returned. The returned value will have its non-ASCII and other control characters percent-encoded, as intoEncoded().

This function was introduced in Qt 4.4.

See alsosetEncodedFragment() andtoEncoded().

QByteArray QUrl::encodedHost() const

Returns the host part of the URL if it is defined; otherwise an empty string is returned.

Note: encodedHost() does not return percent-encoded hostnames. Instead, the ACE-encoded (bare ASCII in Punycode encoding) form will be returned for any non-ASCII hostname.

This function is equivalent to callingQUrl::toAce() on the return value ofhost().

This function was introduced in Qt 4.4.

See alsosetEncodedHost().

QByteArray QUrl::encodedPassword() const

Returns the password of the URL if it is defined; otherwise an empty string is returned. The returned value will have its non-ASCII and other control characters percent-encoded, as intoEncoded().

This function was introduced in Qt 4.4.

See alsosetEncodedPassword() andtoEncoded().

QByteArray QUrl::encodedPath() const

Returns the path of the URL if it is defined; otherwise an empty string is returned. The returned value will have its non-ASCII and other control characters percent-encoded, as intoEncoded().

This function was introduced in Qt 4.4.

See alsosetEncodedPath() andtoEncoded().

QByteArray QUrl::encodedQuery() const

Returns the query string of the URL in percent encoded form.

See alsosetEncodedQuery().

QByteArray QUrl::encodedQueryItemValue(constQByteArray & key) const

Returns the first query string value whose key is equal tokey from the URL.

Note: if the encodedkey does not match the encoded version of the query, this function will not work. That is, if the encoded query of this URL is "search=Qt%20Rules", calling this function withkey = "%73earch" will return an empty string.

This function was introduced in Qt 4.4.

See alsoqueryItemValue() andallQueryItemValues().

QList<QPair<QByteArray,QByteArray> > QUrl::encodedQueryItems() const

Returns the query string of the URL, as a map of encoded keys and values.

This function was introduced in Qt 4.4.

See alsosetEncodedQueryItems(),setQueryItems(), andsetEncodedQuery().

QByteArray QUrl::encodedUserName() const

Returns the user name of the URL if it is defined; otherwise an empty string is returned. The returned value will have its non-ASCII and other control characters percent-encoded, as intoEncoded().

This function was introduced in Qt 4.4.

See alsosetEncodedUserName().

QString QUrl::errorString() const

Returns a text string that explains why an URL is invalid in the case being; otherwise returns an empty string.

This function was introduced in Qt 4.2.

QString QUrl::fragment() const

Returns the fragment of the URL.

See alsosetFragment().

[static]QString QUrl::fromAce(constQByteArray & domain)

Returns the Unicode form of the given domain namedomain, which is encoded in the ASCII Compatible Encoding (ACE). The result of this function is considered equivalent todomain.

If the value indomain cannot be encoded, it will be converted toQString and returned.

The ASCII Compatible Encoding (ACE) is defined by RFC 3490, RFC 3491 and RFC 3492. It is part of the Internationalizing Domain Names in Applications (IDNA) specification, which allows for domain names (like"example.com") to be written using international characters.

This function was introduced in Qt 4.2.

[static]QUrl QUrl::fromEncoded(constQByteArray & input)

Parsesinput and returns the correspondingQUrl.input is assumed to be in encoded form, containing only ASCII characters.

The URL is parsed usingTolerantMode.

See alsotoEncoded() andsetUrl().

[static]QUrl QUrl::fromEncoded(constQByteArray & input,ParsingMode parsingMode)

This is an overloaded function.

Parses the URL usingparsingMode.

See alsotoEncoded() andsetUrl().

[static]QUrl QUrl::fromLocalFile(constQString & localFile)

Returns aQUrl representation oflocalFile, interpreted as a local file. This function accepts paths separated by slashes as well as the native separator for this platform.

This function also accepts paths with a doubled leading slash (or backslash) to indicate a remote file, as in "//servername/path/to/file.txt". Note that only certain platforms can actually open this file usingQFile::open().

See alsotoLocalFile(),isLocalFile(), andQDir::toNativeSeparators().

[static]QString QUrl::fromPercentEncoding(constQByteArray & input)

Returns a decoded copy ofinput.input is first decoded from percent encoding, then converted from UTF-8 to unicode.

[static]QUrl QUrl::fromUserInput(constQString & userInput)

Returns a valid URL from a user supplieduserInput string if one can be deducted. In the case that is not possible, an invalidQUrl() is returned.

Most applications that can browse the web, allow the user to input a URL in the form of a plain string. This string can be manually typed into a location bar, obtained from the clipboard, or passed in via command line arguments.

When the string is not already a valid URL, a best guess is performed, making various web related assumptions.

In the case the string corresponds to a valid file path on the system, a file:// URL is constructed, usingQUrl::fromLocalFile().

If that is not the case, an attempt is made to turn the string into a http:// or ftp:// URL. The latter in the case the string starts with 'ftp'. The result is then passed throughQUrl's tolerant parser, and in the case or success, a validQUrl is returned, or else aQUrl().

Examples:

  • www.qt.io becomes http://www.qt.io
  • ftp.qt.io becomes ftp://ftp.qt.io
  • hostname becomes http://hostname
  • /home/user/test.html becomes file:///home/user/test.html

This function was introduced in Qt 4.6.

bool QUrl::hasEncodedQueryItem(constQByteArray & key) const

Returns true if there is a query string pair whose key is equal tokey from the URL.

Note: if the encodedkey does not match the encoded version of the query, this function will return false. That is, if the encoded query of this URL is "search=Qt%20Rules", calling this function withkey = "%73earch" will return false.

This function was introduced in Qt 4.4.

See alsohasQueryItem().

bool QUrl::hasFragment() const

Returns true if this URL contains a fragment (i.e., if # was seen on it).

This function was introduced in Qt 4.2.

See alsofragment() andsetFragment().

bool QUrl::hasQuery() const

Returns true if this URL contains a Query (i.e., if ? was seen on it).

This function was introduced in Qt 4.2.

See alsohasQueryItem() andencodedQuery().

bool QUrl::hasQueryItem(constQString & key) const

Returns true if there is a query string pair whose key is equal tokey from the URL.

See alsohasEncodedQueryItem().

QString QUrl::host() const

Returns the host of the URL if it is defined; otherwise an empty string is returned.

See alsosetHost().

[static]QStringList QUrl::idnWhitelist()

Returns the current whitelist of top-level domains that are allowed to have non-ASCII characters in their compositions.

SeesetIdnWhitelist() for the rationale of this list.

This function was introduced in Qt 4.2.

See alsosetIdnWhitelist().

bool QUrl::isEmpty() const

Returns true if the URL has no data; otherwise returns false.

bool QUrl::isLocalFile() const

Returns true if this URL is pointing to a local file path. A URL is a local file path if the scheme is "file".

Note that this function considers URLs with hostnames to be local file paths, even if the eventual file path cannot be opened withQFile::open().

This function was introduced in Qt 4.8.

See alsofromLocalFile() andtoLocalFile().

bool QUrl::isParentOf(constQUrl & childUrl) const

Returns true if this URL is a parent ofchildUrl.childUrl is a child of this URL if the two URLs share the same scheme and authority, and this URL's path is a parent of the path ofchildUrl.

bool QUrl::isRelative() const

Returns true if the URL is relative; otherwise returns false. A URL is relative if its scheme is undefined; this function is therefore equivalent to callingscheme().isEmpty().

bool QUrl::isValid() const

Returns true if the URL is valid; otherwise returns false.

The URL is run through a conformance test. Every part of the URL must conform to the standard encoding rules of the URI standard for the URL to be reported as valid.

bool checkUrl(constQUrl&url) {if (!url.isValid()) {qDebug(QString("Invalid URL: %1").arg(url.toString()));returnfalse;    }returntrue;}

QString QUrl::password() const

Returns the password of the URL if it is defined; otherwise an empty string is returned.

See alsosetPassword().

QString QUrl::path() const

Returns the path of the URL.

See alsosetPath().

int QUrl::port() const

Returns the port of the URL, or -1 if the port is unspecified.

See alsosetPort().

int QUrl::port(int defaultPort) const

This is an overloaded function.

Returns the port of the URL, ordefaultPort if the port is unspecified.

Example:

QFtp ftp;ftp.connectToHost(url.host(), url.port(21));

This function was introduced in Qt 4.1.

QString QUrl::queryItemValue(constQString & key) const

Returns the first query string value whose key is equal tokey from the URL.

Note:This method does not decode spaces plus (+) signs as spaces (ASCII 0x20), like HTML forms do. If you need that kind of decoding, you must useQUrl::encodedQueryItemValue and decode the data yourself.

See alsoallQueryItemValues().

QList<QPair<QString,QString> > QUrl::queryItems() const

Returns the query string of the URL, as a map of keys and values.

Note:This method does not decode spaces plus (+) signs as spaces (ASCII 0x20), like HTML forms do. If you need that kind of decoding, you must useQUrl::encodedQueryItems and decode the data yourself.

See alsosetQueryItems() andsetEncodedQuery().

char QUrl::queryPairDelimiter() const

Returns the character used to delimit between key-value pairs in the query string of the URL.

char QUrl::queryValueDelimiter() const

Returns the character used to delimit between keys and values in the query string of the URL.

void QUrl::removeAllEncodedQueryItems(constQByteArray & key)

Removes all the query string pairs whose key is equal tokey from the URL.

Note: if the encodedkey does not match the encoded version of the query, this function will not work. That is, if the encoded query of this URL is "search=Qt%20Rules", calling this function withkey = "%73earch" will do nothing.

This function was introduced in Qt 4.4.

See alsoremoveQueryItem().

void QUrl::removeAllQueryItems(constQString & key)

Removes all the query string pairs whose key is equal tokey from the URL.

See alsoremoveQueryItem().

void QUrl::removeEncodedQueryItem(constQByteArray & key)

Removes the first query string pair whose key is equal tokey from the URL.

Note: if the encodedkey does not match the encoded version of the query, this function will not work. That is, if the encoded query of this URL is "search=Qt%20Rules", calling this function withkey = "%73earch" will do nothing.

This function was introduced in Qt 4.4.

See alsoremoveQueryItem() andremoveAllQueryItems().

void QUrl::removeQueryItem(constQString & key)

Removes the first query string pair whose key is equal tokey from the URL.

See alsoremoveAllQueryItems().

QUrl QUrl::resolved(constQUrl & relative) const

Returns the result of the merge of this URL withrelative. This URL is used as a base to convertrelative to an absolute URL.

Ifrelative is not a relative URL, this function will returnrelative directly. Otherwise, the paths of the two URLs are merged, and the new URL returned has the scheme and authority of the base URL, but with the merged path, as in the following example:

QUrl baseUrl("http://qt.nokia.com/support");QUrl relativeUrl("../products/solutions");qDebug(baseUrl.resolved(relativeUrl).toString());// prints "http://qt.nokia.com/products/solutions"

Calling resolved() with ".." returns aQUrl whose directory is one level higher than the original. Similarly, calling resolved() with "../.." removes two levels from the path. Ifrelative is "/", the path becomes "/".

See alsoisRelative().

QString QUrl::scheme() const

Returns the scheme of the URL. If an empty string is returned, this means the scheme is undefined and the URL is then relative.

See alsosetScheme() andisRelative().

void QUrl::setAuthority(constQString & authority)

Sets the authority of the URL toauthority.

The authority of a URL is the combination of user info, a host name and a port. All of these elements are optional; an empty authority is therefore valid.

The user info and host are separated by a '@', and the host and port are separated by a ':'. If the user info is empty, the '@' must be omitted; although a stray ':' is permitted if the port is empty.

The following example shows a valid authority string:

See alsoauthority().

void QUrl::setEncodedFragment(constQByteArray & fragment)

Sets the URL's fragment to the percent-encodedfragment. The fragment is the last part of the URL, represented by a '#' followed by a string of characters. It is typically used in HTTP for referring to a certain link or point on a page:

The fragment is sometimes also referred to as the URL "reference".

Passing an argument of QByteArray() (a nullQByteArray) will unset the fragment. Passing an argument ofQByteArray("") (an empty but not nullQByteArray) will set the fragment to an empty string (as if the original URL had a lone "#").

This function was introduced in Qt 4.4.

See alsosetFragment() andencodedFragment().

void QUrl::setEncodedHost(constQByteArray & host)

Sets the URL's host to the ACE- or percent-encodedhost. Thehost is part of the user info element in the authority of the URL, as described insetAuthority().

This function was introduced in Qt 4.4.

See alsosetHost(),encodedHost(),setAuthority(), andfromAce().

void QUrl::setEncodedPassword(constQByteArray & password)

Sets the URL's password to the percent-encodedpassword. Thepassword is part of the user info element in the authority of the URL, as described insetUserInfo().

Note: this function does not verify thatpassword is properly encoded. It is the caller's responsibility to ensure that the any delimiters (such as colons or slashes) are properly encoded.

This function was introduced in Qt 4.4.

See alsosetPassword(),encodedPassword(), andsetUserInfo().

void QUrl::setEncodedPath(constQByteArray & path)

Sets the URL's path to the percent-encodedpath. The path is the part of the URL that comes after the authority but before the query string.

For non-hierarchical schemes, the path will be everything following the scheme declaration, as in the following example:

Note: this function does not verify thatpath is properly encoded. It is the caller's responsibility to ensure that the any delimiters (such as '?' and '#') are properly encoded.

This function was introduced in Qt 4.4.

See alsosetPath(),encodedPath(), andsetUserInfo().

void QUrl::setEncodedQuery(constQByteArray & query)

Sets the query string of the URL toquery. The string is inserted as-is, and no further encoding is performed when callingtoEncoded().

This function is useful if you need to pass a query string that does not fit into the key-value pattern, or that uses a different scheme for encoding special characters than what is suggested byQUrl.

Passing a value of QByteArray() toquery (a nullQByteArray) unsets the query completely. However, passing a value ofQByteArray("") will set the query to an empty value, as if the original URL had a lone "?".

See alsoencodedQuery() andhasQuery().

void QUrl::setEncodedQueryItems(constQList<QPair<QByteArray,QByteArray> > & query)

Sets the query string of the URL to the encoded version ofquery. The contents ofquery are converted to a string internally, each pair delimited by the character returned by pairDelimiter(), and the key and value are delimited by valueDelimiter().

Note: this function does not verify that the key-value pairs are properly encoded. It is the caller's responsibility to ensure that the query delimiters are properly encoded, if any.

This function was introduced in Qt 4.4.

See alsosetQueryDelimiters(),encodedQueryItems(), andsetQueryItems().

void QUrl::setEncodedUrl(constQByteArray & encodedUrl)

Constructs a URL by parsing the contents ofencodedUrl.

encodedUrl is assumed to be a URL string in percent encoded form, containing only ASCII characters.

UseisValid() to determine if a valid URL was constructed.

See alsosetUrl().

void QUrl::setEncodedUrl(constQByteArray & encodedUrl,ParsingMode parsingMode)

Constructs a URL by parsing the contents ofencodedUrl using the givenparsingMode.

void QUrl::setEncodedUserName(constQByteArray & userName)

Sets the URL's user name to the percent-encodeduserName. TheuserName is part of the user info element in the authority of the URL, as described insetUserInfo().

Note: this function does not verify thatuserName is properly encoded. It is the caller's responsibility to ensure that the any delimiters (such as colons or slashes) are properly encoded.

This function was introduced in Qt 4.4.

See alsosetUserName(),encodedUserName(), andsetUserInfo().

void QUrl::setFragment(constQString & fragment)

Sets the fragment of the URL tofragment. The fragment is the last part of the URL, represented by a '#' followed by a string of characters. It is typically used in HTTP for referring to a certain link or point on a page:

The fragment is sometimes also referred to as the URL "reference".

Passing an argument of QString() (a nullQString) will unset the fragment. Passing an argument ofQString("") (an empty but not nullQString) will set the fragment to an empty string (as if the original URL had a lone "#").

See alsofragment() andhasFragment().

void QUrl::setHost(constQString & host)

Sets the host of the URL tohost. The host is part of the authority.

See alsohost() andsetAuthority().

[static]void QUrl::setIdnWhitelist(constQStringList & list)

Sets the whitelist of Top-Level Domains (TLDs) that are allowed to have non-ASCII characters in domains to the value oflist.

Note that if you call this function, you need to do so\em before you start any threads that might accessidnWhitelist().

Qt has comes a default list that contains the Internet top-level domains that have published support for Internationalized Domain Names (IDNs) and rules to guarantee that no deception can happen between similarly-looking characters (such as the Latin lowercase letter'a' and the Cyrillic equivalent, which in most fonts are visually identical).

This list is periodically maintained, as registrars publish new rules.

This function is provided for those who need to manipulate the list, in order to add or remove a TLD. It is not recommended to change its value for purposes other than testing, as it may expose users to security risks.

This function was introduced in Qt 4.2.

See alsoidnWhitelist().

void QUrl::setPassword(constQString & password)

Sets the URL's password topassword. Thepassword is part of the user info element in the authority of the URL, as described insetUserInfo().

See alsopassword() andsetUserInfo().

void QUrl::setPath(constQString & path)

Sets the path of the URL topath. The path is the part of the URL that comes after the authority but before the query string.

For non-hierarchical schemes, the path will be everything following the scheme declaration, as in the following example:

See alsopath().

void QUrl::setPort(int port)

Sets the port of the URL toport. The port is part of the authority of the URL, as described insetAuthority().

port must be between 0 and 65535 inclusive. Setting the port to -1 indicates that the port is unspecified.

See alsoport().

void QUrl::setQueryDelimiters(char valueDelimiter,char pairDelimiter)

Sets the characters used for delimiting between keys and values, and between key-value pairs in the URL's query string. The default value delimiter is '=' and the default pair delimiter is '&'.

valueDelimiter will be used for separating keys from values, andpairDelimiter will be used to separate key-value pairs. Any occurrences of these delimiting characters in the encoded representation of the keys and values of the query string are percent encoded.

IfvalueDelimiter is set to '-' andpairDelimiter is '/', the above query string would instead be represented like this:

http://www.example.com/cgi-bin/drawgraph.cgi?type-pie/color-green

Calling this function does not change the delimiters of the current query string. It only affectsqueryItems(),setQueryItems() and addQueryItems().

void QUrl::setQueryItems(constQList<QPair<QString,QString> > & query)

Sets the query string of the URL to an encoded version ofquery. The contents ofquery are converted to a string internally, each pair delimited by the character returned by pairDelimiter(), and the key and value are delimited by valueDelimiter().

Note:This method does not encode spaces (ASCII 0x20) as plus (+) signs, like HTML forms do. If you need that kind of encoding, you must encode the value yourself and useQUrl::setEncodedQueryItems.

See alsosetQueryDelimiters(),queryItems(), andsetEncodedQueryItems().

void QUrl::setScheme(constQString & scheme)

Sets the scheme of the URL toscheme. As a scheme can only contain ASCII characters, no conversion or encoding is done on the input.

The scheme describes the type (or protocol) of the URL. It's represented by one or more ASCII characters at the start the URL, and is followed by a ':'. The following example shows a URL where the scheme is "ftp":

The scheme can also be empty, in which case the URL is interpreted as relative.

See alsoscheme() andisRelative().

void QUrl::setUrl(constQString & url)

Constructs a URL by parsing the contents ofurl.

url is assumed to be in unicode format, with no percent encoding.

CallingisValid() will tell whether or not a valid URL was constructed.

See alsosetEncodedUrl().

void QUrl::setUrl(constQString & url,ParsingMode parsingMode)

This is an overloaded function.

Parsesurl using the parsing modeparsingMode.

See alsosetEncodedUrl().

void QUrl::setUserInfo(constQString & userInfo)

Sets the user info of the URL touserInfo. The user info is an optional part of the authority of the URL, as described insetAuthority().

The user info consists of a user name and optionally a password, separated by a ':'. If the password is empty, the colon must be omitted. The following example shows a valid user info string:

See alsouserInfo(),setUserName(),setPassword(), andsetAuthority().

void QUrl::setUserName(constQString & userName)

Sets the URL's user name touserName. TheuserName is part of the user info element in the authority of the URL, as described insetUserInfo().

See alsosetEncodedUserName(),userName(), andsetUserInfo().

void QUrl::swap(QUrl & other)

Swaps URLother with this URL. This operation is very fast and never fails.

This function was introduced in Qt 4.8.

[static]QByteArray QUrl::toAce(constQString & domain)

Returns the ASCII Compatible Encoding of the given domain namedomain. The result of this function is considered equivalent todomain.

The ASCII-Compatible Encoding (ACE) is defined by RFC 3490, RFC 3491 and RFC 3492. It is part of the Internationalizing Domain Names in Applications (IDNA) specification, which allows for domain names (like"example.com") to be written using international characters.

This function return an empty QByteArra ifdomain is not a valid hostname. Note, in particular, that IPv6 literals are not valid domain names.

This function was introduced in Qt 4.2.

QByteArray QUrl::toEncoded(FormattingOptions options = None) const

Returns the encoded representation of the URL if it's valid; otherwise an emptyQByteArray is returned. The output can be customized by passing flags withoptions.

The user info, path and fragment are all converted to UTF-8, and all non-ASCII characters are then percent encoded. The host name is encoded using Punycode.

QString QUrl::toLocalFile() const

Returns the path of this URL formatted as a local file path. The path returned will use forward slashes, even if it was originally created from one with backslashes.

If this URL contains a non-empty hostname, it will be encoded in the returned value in the form found on SMB networks (for example, "//servername/path/to/file.txt").

If this is a relative URL, in Qt 4.x this function returns the path to maintain backward compatability. This will change from 5.0 onwards. Then the path is returned only for URLs where the scheme is "file", and for all other URLs an empty string is returned.

See alsofromLocalFile() andisLocalFile().

[static]QByteArray QUrl::toPercentEncoding(constQString & input, constQByteArray & exclude = QByteArray(), constQByteArray & include = QByteArray())

Returns an encoded copy ofinput.input is first converted to UTF-8, and all ASCII-characters that are not in the unreserved group are percent encoded. To prevent characters from being percent encoded pass them toexclude. To force characters to be percent encoded pass them toinclude.

Unreserved is defined as: ALPHA / DIGIT / "-" / "." / "_" / "~"

QByteArray ba=QUrl::toPercentEncoding("{a fishy string?}","{}","s");qDebug(ba.constData());// prints "{a fi%73hy %73tring%3F}"

QString QUrl::toString(FormattingOptions options = None) const

Returns the human-displayable string representation of the URL. The output can be customized by passing flags withoptions.

See alsoFormattingOptions andtoEncoded().

QString QUrl::topLevelDomain() const

Returns the TLD (Top-Level Domain) of the URL, (e.g. .co.uk, .net). Note that the return value is prefixed with a '.' unless the URL does not contain a valid TLD, in which case the function returns an empty string.

This function was introduced in Qt 4.8.

QString QUrl::userInfo() const

Returns the user info of the URL, or an empty string if the user info is undefined.

See alsosetUserInfo().

QString QUrl::userName() const

Returns the user name of the URL if it is defined; otherwise an empty string is returned.

See alsosetUserName() andencodedUserName().

bool QUrl::operator!=(constQUrl & url) const

Returns true if this URL and the givenurl are not equal; otherwise returns false.

QUrl & QUrl::operator=(constQUrl & url)

Assigns the specifiedurl to this object.

QUrl & QUrl::operator=(constQString & url)

Assigns the specifiedurl to this object.

QUrl & QUrl::operator=(QUrl && other)

bool QUrl::operator==(constQUrl & url) const

Returns true if this URL and the givenurl are equal; otherwise returns false.

Related Non-Members

uintqHash(constQUrl & url)

Computes a hash key from the normalized version ofurl.

This function was introduced in Qt 4.7.

QDataStream &operator<<(QDataStream & out, constQUrl & url)

Writes urlurl to the streamout and returns a reference to the stream.

See alsoFormat of the QDataStream operators.

QDataStream &operator>>(QDataStream & in,QUrl & url)

Reads a url intourl from the streamin and returns a reference to the stream.

See alsoFormat of the QDataStream operators.

Macro Documentation

QT_NO_URL_CAST_FROM_STRING

Disables automatic conversions fromQString (or char *) toQUrl.

Compiling your code with this define is useful when you have a lot of code that usesQString for file names and you wish to convert it to useQUrl for network transparency. In any code that usesQUrl, it can help avoid missingQUrl::resolved() calls, and other misuses ofQString toQUrl conversions.

For example, if you have code like

url = filename; // probably not what you want

you can rewrite it as

url=QUrl::fromLocalFile(filename);url= baseurl.resolved(QUrl(filename));

See alsoQT_NO_CAST_FROM_ASCII.

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp