URL Shortener

From Qt Wiki
Jump to: navigation, search


URL shortening is a technique in which a Uniform Resource Locator (URL) may be made substantially shorter in length and still direct to the required page. It is often used for simpler presentation of URLs at social networks and mobile applications.

URL Shortener API

There are plenty of URL Shortner API. The most popular are:

URL Shortener for Qt

Qt implementation of URL Shortener based on is.gd service and the file download class.

Source Code

URL Shortener API provided by is.gd supports json, xml and plain text as a simple response. In certain cases such as mobile applications the simple response is better solution because parsing is not required.

  1. ifndef URLSHORTENER_H
  2. define URLSHORTENER_H

//Standard includes

  1. include <QObject>
  2. include <QString>

//Project specific includes

  1. include "filedownloader.h"

class UrlShortener : public QObject {


private: FileDownloader* m_pDownloader; QString m_sShortUrl;

public: explicit UrlShortener(QObject *parent = 0); virtual ~UrlShortener();

* Request short URL from Google API
* param sURL
* return nothing
void requestShortUrl(QString sURL);
* Get address
* return QString
QString getShortUrl() const;


void shortUrlRetrieved();

private slots: void parseResponse(); };

  1. endif // URLSHORTENER_H

//Project specific includes

  1. include "urlshortener.h"

//Standard includes

  1. include <QTextStream>

UrlShortener::UrlShortener(QObject *parent) :



m_pDownloader = new FileDownloader(this);
connect(m_pDownloader, SIGNAL (downloaded()), SLOT (parseResponse()));

} // ------------------

UrlShortener::~UrlShortener() {

//Nothing to do

} // ------------------

void UrlShortener::requestShortUrl(QString sURL) {

//Obtain short URL
QUrl req = QUrl( "http://is.gd/create.php");
req.addQueryItem("format", "simple");
req.addQueryItem("longurl", sURL);

} // ------------------

void UrlShortener::parseResponse() {

QTextStream downloadedStream(m_pDownloader->downloadedData());
m_sShortUrl = downloadedStream.readAll();
//emit a signal
emit shortUrlRetrieved();

} // ------------------

QString UrlShortener::getShortUrl() const {

return m_sShortUrl;

} // ------------------


m_pUrlShortener = new UrlShortener(this); connect(m_pUrlShortener, SIGNAL (shortUrlRetrieved()), this, SLOT (loadMapShortUrl())); //* m_pUrlShortener->requestShortUrl(QString("http://example.com"));