Q3Url Class

The Q3Url class provides a URL parser and simplifies working with URLs. 更多...

头: #include <Q3Url>

公共函数

Q3Url ()
Q3Url (const QString & url )
Q3Url (const Q3Url & url )
Q3Url (const Q3Url & url , const QString & relUrl , bool checkSlash = false)
virtual ~Q3Url ()
virtual void addPath (const QString & pa )
virtual bool cdUp ()
QString dirPath () const
QString encodedPathAndQuery ()
QString fileName () const
bool hasHost () const
bool hasPassword () const
bool hasPath () const
bool hasPort () const
bool hasRef () const
bool hasUser () const
QString host () const
bool isLocalFile () const
bool isValid () const
QString password () const
QString path (bool correct = true) const
int port () const
QString protocol () const
QString query () const
QString ref () const
virtual void setEncodedPathAndQuery (const QString & pathAndQuery )
virtual void setFileName (const QString & name )
virtual void setHost (const QString & host )
virtual void setPassword (const QString & pass )
virtual void setPath (const QString & path )
virtual void setPort (int port )
virtual void setProtocol (const QString & protocol )
virtual void setQuery (const QString & txt )
virtual void setRef (const QString & txt )
virtual void setUser (const QString & user )
virtual QString toString (bool encodedPath = false, bool forcePrependProtocol = true) const
QString user () const
operator QString () const
Q3Url & operator= (const Q3Url & url )
Q3Url & operator= (const QString & url )
bool operator== (const Q3Url & url ) const
bool operator== (const QString & url ) const

静态公共成员

void decode (QString & url )
void encode (QString & url )
bool isRelativeUrl (const QString & url )

保护函数

virtual bool parse (const QString & url )
virtual void reset ()

详细描述

The Q3Url class provides a URL parser and simplifies working with URLs.

The Q3Url class is provided for simple work with URLs. It can parse, decode, encode, etc.

Q3Url works with the decoded path and encoded query in turn.

范例:

<tt>http://example.com:80/cgi-bin/test%20me.pl?cmd=Hello%20you</tt>

函数 返回
protocol () "http"
host () "example.com"
port () 80
path () "/cgi-bin/test me.pl"
fileName () "test me.pl"
query () "cmd=Hello%20you"

范例:

<tt>http://doc.qt.io/qt-4.8/qdockwidget.html#widget</tt>

函数 返回
protocol () "http"
host () "doc.qt.io"
fileName () "qt-4.8/qdockwidget.html"
ref () "widget"

The individual parts of a URL can be set with setProtocol (), setHost (), setPort (), setPath (), setFileName (), setRef () 和 setQuery (). A URL could contain, for example, an ftp address which requires a user name and password; these can be set with setUser () 和 setPassword ().

Because path is always encoded internally you must not use "%00" in the path, although this is okay (but not recommended) for the query.

Q3Url is normally used like this:

Q3Url url( "http://qt.nokia.com" );
// or
Q3Url url( "file:///home/myself/Mail", "Inbox" );
					

You can then access and manipulate the various parts of the URL.

To make it easy to work with Q3Urls and QStrings, Q3Url implements the necessary cast and assignment operators so you can do following:

Q3Url url( "http://qt.nokia.com" );
QString s = url;
// or
QString s( "http://qt.nokia.com" );
Q3Url url( s );
					

Use the static functions, encode () 和 decode () to encode or decode a URL in a string. (They operate on the string in-place.) The isRelativeUrl () static function returns true if the given string is a relative URL.

If you want to use a URL to work on a hierarchical structure (e.g. a local or remote filesystem), you might want to use the subclass Q3UrlOperator.

另请参阅 Q3UrlOperator.

成员函数文档编制

Q3Url:: Q3Url ()

Constructs an empty URL that is invalid.

Q3Url:: Q3Url (const QString & url )

Constructs a URL by parsing the string url .

If you pass a string like "/home/qt", the "file" protocol is assumed.

Q3Url:: Q3Url (const Q3Url & url )

Copy constructor. Copies the data of url .

Q3Url:: Q3Url (const Q3Url & url , const QString & relUrl , bool checkSlash = false)

Constructs an URL taking url as the base (context) and relUrl as a relative URL to url 。若 relUrl is not relative, relUrl is taken as the new URL.

For example, the path of

Q3Url url( "ftp://ftp.qt.nokia.com/qt/source", "qt-2.1.0.tar.gz" );
					

will be "/qt/srource/qt-2.1.0.tar.gz".

On the other hand,

Q3Url url( "ftp://ftp.qt.nokia.com/qt/source", "/usr/local" );
					

will result in a new URL, "ftp://ftp.qt.nokia.com/usr/local", because "/usr/local" isn't relative.

Similarly,

Q3Url url( "ftp://ftp.qt.nokia.com/qt/source", "file:///usr/local" );
					

will result in a new URL, with "/usr/local" as the path and "file" as the protocol.

Normally it is expected that the path of url points to a directory, even if the path has no slash at the end. But if you want the constructor to handle the last part of the path as a file name if there is no slash at the end, and to let it be replaced by the file name of relUrl (if it contains one), set checkSlash to true.

[虚拟] Q3Url:: ~Q3Url ()

析构函数。

[虚拟] void Q3Url:: addPath (const QString & pa )

Adds the path pa to the path of the URL.

另请参阅 setPath () 和 hasPath ().

[虚拟] bool Q3Url:: cdUp ()

Changes the directory to one directory up. This function always returns true.

另请参阅 setPath ().

[static] void Q3Url:: decode ( QString & url )

Decodes the url in-place into UTF-8. For example

QString url = "http%3A//qt%20nokia%20com"
Q3Url::decode( url );
// url is now "http://qt.nokia.com"
					

另请参阅 encode ().

QString Q3Url:: dirPath () const

Returns the directory path of the URL. This is the part of the path of the URL without the fileName (). See the documentation of fileName () for a discussion of what is handled as file name and what is handled as directory path.

另请参阅 setPath () 和 hasPath ().

[static] void Q3Url:: encode ( QString & url )

Encodes the url in-place into UTF-8. For example

QString url = http://qt.nokia.com
Q3Url::encode( url );
// url is now "http%3A//qt%20nokia%20com"
					

另请参阅 decode ().

QString Q3Url:: encodedPathAndQuery ()

Returns the encoded path and query.

另请参阅 setEncodedPathAndQuery () 和 decode ().

QString Q3Url:: fileName () const

Returns the file name of the URL. If the path of the URL doesn't have a slash at the end, the part between the last slash and the end of the path string is considered to be the file name. If the path has a slash at the end, an empty string is returned here.

另请参阅 setFileName ().

bool Q3Url:: hasHost () const

Returns true if the URL contains a hostname; otherwise returns false.

另请参阅 setHost ().

bool Q3Url:: hasPassword () const

Returns true if the URL contains a password; otherwise returns false.

警告: Passwords passed in URLs are normally insecure ; this is due to the mechanism, not because of Qt.

另请参阅 setPassword () 和 setUser ().

bool Q3Url:: hasPath () const

Returns true if the URL contains a path; otherwise returns false.

另请参阅 path () 和 setPath ().

bool Q3Url:: hasPort () const

Returns true if the URL contains a port; otherwise returns false.

另请参阅 setPort ().

bool Q3Url:: hasRef () const

Returns true if the URL has a reference; otherwise returns false.

另请参阅 setRef ().

bool Q3Url:: hasUser () const

Returns true if the URL contains a username; otherwise returns false.

另请参阅 setUser () 和 setPassword ().

QString Q3Url:: host () const

Returns the hostname of the URL.

另请参阅 setHost () 和 hasHost ().

bool Q3Url:: isLocalFile () const

Returns true if the URL is a local file; otherwise returns false.

[static] bool Q3Url:: isRelativeUrl (const QString & url )

返回 true 若 url is relative; otherwise returns false.

bool Q3Url:: isValid () const

Returns true if the URL is valid; otherwise returns false. A URL is invalid if it cannot be parsed, for example.

[virtual protected] bool Q3Url:: parse (const QString & url )

剖析 url . Returns true on success; otherwise returns false.

QString Q3Url:: password () const

Returns the password of the URL.

警告: Passwords passed in URLs are normally insecure ; this is due to the mechanism, not because of Qt.

另请参阅 setPassword () 和 setUser ().

QString Q3Url:: path ( bool correct = true) const

Returns the path of the URL. If correct is true, the path is cleaned (deals with too many or too few slashes, cleans things like "/../..", etc). Otherwise path() returns exactly the path that was parsed or set.

另请参阅 setPath () 和 hasPath ().

int Q3Url:: port () const

Returns the port of the URL or -1 if no port has been set.

另请参阅 setPort ().

QString Q3Url:: protocol () const

Returns the protocol of the URL. Typically, "file", "http", "ftp", etc.

另请参阅 setProtocol ().

QString Q3Url:: query () const

Returns the (encoded) query of the URL.

另请参阅 setQuery () 和 decode ().

QString Q3Url:: ref () const

Returns the (encoded) reference of the URL.

另请参阅 setRef (), hasRef (),和 decode ().

[virtual protected] void Q3Url:: reset ()

Resets all parts of the URL to their default values and invalidates it.

[虚拟] void Q3Url:: setEncodedPathAndQuery (const QString & pathAndQuery )

剖析 pathAndQuery for a path and query and sets those values. The whole string must be encoded.

另请参阅 encodedPathAndQuery () 和 encode ().

[虚拟] void Q3Url:: setFileName (const QString & name )

Sets the file name of the URL to name . If this URL contains a fileName (), the original file name is replaced by name .

See the documentation of fileName () for a more detailed discussion of what is handled as file name and what is handled as a directory path.

另请参阅 fileName ().

[虚拟] void Q3Url:: setHost (const QString & host )

Sets the hostname of the URL to host .

另请参阅 host () 和 hasHost ().

[虚拟] void Q3Url:: setPassword (const QString & pass )

Sets the password of the URL to pass .

警告: Passwords passed in URLs are normally insecure ; this is due to the mechanism, not because of Qt.

另请参阅 password () 和 setUser ().

[虚拟] void Q3Url:: setPath (const QString & path )

将 URL 路径设为 path .

另请参阅 path () 和 hasPath ().

[虚拟] void Q3Url:: setPort ( int port )

将 URL 端口设为 port .

另请参阅 port ().

[虚拟] void Q3Url:: setProtocol (const QString & protocol )

Sets the protocol of the URL to protocol . Typically, "file", "http", "ftp", etc.

另请参阅 protocol ().

[虚拟] void Q3Url:: setQuery (const QString & txt )

Sets the query of the URL to txt . txt must be encoded.

另请参阅 query () 和 encode ().

[虚拟] void Q3Url:: setRef (const QString & txt )

Sets the reference of the URL to txt . txt must be encoded.

另请参阅 ref (), hasRef (),和 encode ().

[虚拟] void Q3Url:: setUser (const QString & user )

Sets the username of the URL to user .

另请参阅 user () 和 setPassword ().

[虚拟] QString Q3Url:: toString ( bool encodedPath = false, bool forcePrependProtocol = true) const

Composes a string version of the URL and returns it. If encodedPath is true the path in the returned string is encoded. If forcePrependProtocol is true and encodedPath looks like a local filename, the "file:/" protocol is also prepended.

另请参阅 encode () 和 decode ().

QString Q3Url:: user () const

Returns the username of the URL.

另请参阅 setUser () 和 setPassword ().

Q3Url:: operator QString () const

Composes a string version of the URL and returns it.

另请参阅 Q3Url::toString ().

Q3Url & Q3Url:: operator= (const Q3Url & url )

Assigns the data of url to this class.

Q3Url & Q3Url:: operator= (const QString & url )

这是重载函数。

剖析 url and assigns the resulting data to this class.

If you pass a string like "/home/qt" the "file" protocol will be assumed.

bool Q3Url:: operator== (const Q3Url & url ) const

Compares this URL with url and returns true if they are equal; otherwise returns false.

bool Q3Url:: operator== (const QString & url ) const

这是重载函数。

Compares this URL with url . url is parsed first. Returns true if url is equal to this url; otherwise returns false.