Qt Toolkit - QUrl Class

| Classes - Annotated - Tree - Functions - Home - Structure

# QUrl Class Reference

The QUrl class provides mainly an URL parser and simplifies working with URLs. More...

`#include <qurl.h>`

Inherited by QUrlOperator.

List of all member functions.

## Public Members

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

## Static Public Members

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

## Protected Members

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

## Detailed Description

The QUrl class provides mainly an URL parser and simplifies working with URLs.

The QUrl class is provided for a easy working with URLs. It does all parsing, decoding, encoding and so on.

Mention that URL has some restrictions regarding the path encoding. URL works intern with the decoded path and and encoded query. For example in

http://localhost/cgi-bin/test%20me.pl?cmd=Hello%20you

would result in a decoded path "/cgi-bin/test me.pl" and in the encoded query "cmd=Hello%20you". Since path is internally always encoded you may NOT use "%00" in the path while this is ok for the query.

QUrl is normally used like that:

      QUrl u( "http://www.trolltech.com" );
      // or
      QUrl u( "file:/home/myself/Mail", "Inbox" );

Then you can access the parts of the URL, change them and do some more stuff.

To allow easy working with QUrl and QString together, QUrl implements the needed cast and assign operators. So you can do following:

      QUrl u( "http://www.trolltech.com" );
      QString s = u;
      // or
      QString s( "http://www.trolltech.com" );
      QUrl u( s );

If you want to use an URL to work on a hierarchical structures (e.g. locally or remote filesystem) the class QUrlOperator, which is derived fro QUrl, may be interesting for you.

See also QUrlOperator.

## Member Function Documentation

### QUrl::QUrl ()

Constructs an empty URL which, is invalid.

### QUrl::QUrl ( const QString & url )

Constructs an URL using url and parses this string.

You can pass strings like "/home/qt", in this case the protocol "file" is assumed.

### QUrl::QUrl ( const QUrl & url )

Copy constructor. Copies the data of url.

### QUrl::QUrl ( const QUrl & url, const QString & relUrl, bool checkSlash = FALSE )

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

For example, the path of

      QUrl u( "ftp://ftp.trolltech.com/qt/source", "qt-2.1.0.tar.gz" );

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

And

      QUrl u( "ftp://ftp.trolltech.com/qt/source", "/usr/local" );

will result in a new URL, "ftp://ftp.trolltech.com/usr/local",

And

      QUrl u( "ftp://ftp.trolltech.com/qt/source", "file:/usr/local" );

will result in a new URL, with "/usr/local" as path and "file" as 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 that the constructor handles the last part of the path as filename, if there is no slash at the end, and let it replace by the filename of relUrl (if it contains one), set checkSlash to TRUE.

### QUrl::~QUrl () `[virtual]`

Destructor.

### QUrl::operator QString () const

Composes a string of the URL and returns it.

See also QUrl::toString().

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

Adds the path pa to the path of the URL.

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

Goes one directory up.

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

Decodes the string url.

### QString QUrl::dirPath () const

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

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

Encodes the string url.

### QString QUrl::encodedPathAndQuery ()

Returns the encoded path plus the query (encoded too).

### QString QUrl::fileName () const

Returns the filename 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 handled as filename. If the path has a slash at the end, an empty string is returned here.

### bool QUrl::hasHost () const

Returns TRUE, if the URL contains a hostname, else FALSE;.

### bool QUrl::hasPassword () const

Returns TRUE, if the URL contains a password, else FALSE;.

### bool QUrl::hasPath () const

Returns TRUE, if the URL contains a path, else FALSE.

### bool QUrl::hasRef () const

Returns TRUE, if the URL has a reference, else it returns FALSE.

### bool QUrl::hasUser () const

Returns TRUE, if the URL contains an username, else FALSE;.

### QString QUrl::host () const

Returns the hostname of the URL.

### bool QUrl::isLocalFile () const

Returns TRUE, if the URL is a local file, else it returns FALSE;.

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

Returns TRUE, if url is relative, else it returns FALSE.

### bool QUrl::isValid () const

Returns TRUE if the URL is valid, else FALSE. An URL is e.g. invalid if there was a parse error.

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

Assign operator. Parses url and assigns the resulting data to this class.

You can pass strings like "/home/qt", in this case the protocol "file" is assumed.

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

Assign operator. Assigns the data of url to this class.

### bool QUrl::operator== ( const QString & url ) const

Compares this URL with url. url is parsed first. Returns TRUE if url is equal to this url, else FALSE:.

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

Compares this URL with url and returns TRUE if they are equal, else FALSE.

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

Parses the url.

### QString QUrl::password () const

Returns the password of the URL.

### QString QUrl::path ( bool correct = TRUE ) const

Returns the path of the URL. If correct is TRUE, the path is cleaned (deals with too many or few slashes, cleans things like "/../..", etc). Else exactly the path which was parsed or set is returned.

### int QUrl::port () const

Returns the port of the URL.

### QString QUrl::protocol () const

Returns the protocol of the URL. It is something like "file" or "ftp".

### QString QUrl::query () const

Returns the query (encoded) of the URL.

### QString QUrl::ref () const

Returns the reference (encoded) of the URL.

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

Resets all values if the URL to its default values and invalidates it.

### void QUrl::setEncodedPathAndQuery ( const QString & path ) `[virtual]`

Sets path and query. Both have to be encoded.

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

Sets the filename of the URL to name. If this url contains a fileName(), this is replaced by name. See the documentation of fileName() for a more detail discussion, about what is handled as file name and what as directory path.

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

Sets the hostname of the URL.

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

Sets the password of the URL.

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

Sets the path or the URL.

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

Sets the port of the URL.

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

Sets the protocol of the URL. This could be e.g. "file", "ftp", or something similar.

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

Sets the query of the URL. Must be encoded.

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

Sets the reference of the URL. Must be encoded.

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

Sets the username of the URL.

### QString QUrl::toString ( bool encodedPath = FALSE, bool forcePrependProtocol = TRUE ) const `[virtual]`

Composes a string of the URL and returns it. If encodedPath is TRUE, the path in the returned string will be encoded. If forcePrependProtocol is TRUE, the protocol (file:/) is also prepended to local filenames, else no protocol is prepended for local filenames.

### QString QUrl::user () const

Returns the username of the URL.

Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2001 Trolltech, all rights reserved.