|  | Home · All Classes · All Functions · Overviews

# Q3Url Class Reference
[Qt3Support module]

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

     #include <Q3Url>

This class is part of the Qt 3 support library. It is provided to keep old source code working. We strongly advise against using it in new code. See Porting to Qt 4 for more information.

Inherited by Q3UrlOperator.

- List of all members, including inherited members

## Public Functions

| 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

## Static Public Members

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

## Protected Functions

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

## Detailed Description

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.

Example:

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

Function | Returns
--- | ---
protocol() | "http"
host() | "example.com"
port() | 80
path() | "/cgi-bin/test&nbsp;me.pl"
fileName() | "test&nbsp;me.pl"
query() | "cmd=Hello%20you"

Example:

<tt>http://qt.nokia.com/doc/qdockarea.html#lines</tt>

Function | Returns
--- | ---
protocol() | "http"
host() | "qt.nokia.com"
fileName() | "doc/qdockarea.html"
ref() | "lines"

The individual parts of a URL can be set with setProtocol(), setHost(), setPort(), setPath(), setFileName(), setRef() and setQuery(). A URL could contain, for example, an ftp address which requires a user name and password; these can be set with setUser() and 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() and 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.

See also Q3UrlOperator.

## Member Function Documentation

### 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. If 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 () `[virtual]`

Destructor.

### void Q3Url::addPath ( const QString & pa ) `[virtual]`

Adds the path pa to the path of the URL.

See also setPath() and hasPath().

### bool Q3Url::cdUp () `[virtual]`

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

See also setPath().

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

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"

See also 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.

See also setPath() and hasPath().

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

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"

See also decode().

### QString Q3Url::encodedPathAndQuery ()

Returns the encoded path and query.

See also setEncodedPathAndQuery() and 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.

See also setFileName().

### bool Q3Url::hasHost () const

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

See also setHost().

### bool Q3Url::hasPassword () const

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

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

See also setPassword() and setUser().

### bool Q3Url::hasPath () const

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

See also path() and setPath().

### bool Q3Url::hasPort () const

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

See also setPort().

### bool Q3Url::hasRef () const

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

See also setRef().

### bool Q3Url::hasUser () const

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

See also setUser() and setPassword().

### QString Q3Url::host () const

Returns the hostname of the URL.

See also setHost() and hasHost().

### bool Q3Url::isLocalFile () const

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

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

Returns true if 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.

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

Parses the url. Returns true on success; otherwise returns false.

### QString Q3Url::password () const

Returns the password of the URL.

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

See also setPassword() and 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.

See also setPath() and hasPath().

### int Q3Url::port () const

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

See also setPort().

### QString Q3Url::protocol () const

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

See also setProtocol().

### QString Q3Url::query () const

Returns the (encoded) query of the URL.

See also setQuery() and decode().

### QString Q3Url::ref () const

Returns the (encoded) reference of the URL.

See also setRef(), hasRef(), and decode().

### void Q3Url::reset () `[virtual protected]`

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

### void Q3Url::setEncodedPathAndQuery ( const QString & pathAndQuery ) `[virtual]`

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

See also encodedPathAndQuery() and encode().

### void Q3Url::setFileName ( const QString & name ) `[virtual]`

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.

See also fileName().

### void Q3Url::setHost ( const QString & host ) `[virtual]`

Sets the hostname of the URL to host.

See also host() and hasHost().

### void Q3Url::setPassword ( const QString & pass ) `[virtual]`

Sets the password of the URL to pass.

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

See also password() and setUser().

### void Q3Url::setPath ( const QString & path ) `[virtual]`

Sets the path of the URL to path.

See also path() and hasPath().

### void Q3Url::setPort ( int port ) `[virtual]`

Sets the port of the URL to port.

See also port().

### void Q3Url::setProtocol ( const QString & protocol ) `[virtual]`

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

See also protocol().

### void Q3Url::setQuery ( const QString & txt ) `[virtual]`

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

See also query() and encode().

### void Q3Url::setRef ( const QString & txt ) `[virtual]`

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

See also ref(), hasRef(), and encode().

### void Q3Url::setUser ( const QString & user ) `[virtual]`

Sets the username of the URL to user.

See also user() and setPassword().

### QString Q3Url::toString ( bool encodedPath = false, bool forcePrependProtocol = true ) const `[virtual]`

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.

See also encode() and decode().

### QString Q3Url::user () const

Returns the username of the URL.

See also setUser() and setPassword().

### Q3Url::operator QString () const

Composes a string version of the URL and returns it.

See also Q3Url::toString().

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

Assigns the data of url to this class.

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

This is an overloaded function.

Parses 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

This is an overloaded function.

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