Shorten your URLs using Qt
Written on July 30, 2009, by Milot Shala.

In today’s micro-blogging shortening URLs is something necessary because of the character limit on the messages, for example Twitter has the message limit of 140 characters (including spaces), while Facebook has the limit of 160 characters (including spaces), for this reason there are several providers that offer URL shortening services which of course they provide web service APIs that allow us programmers to create fun clients around them.

Recently I have seen an arstechnica article describing how to shorten URLs using PyGTK+ with http://is.gd/ service.

I really liked the post and here I will explain how to do something like that but instead of using PyGTK+ I will be using Qt and C++ and instead of http://is.gd/ I will be using my favorite service for shortening URLs, tinyurl.com.

First of all we will start by creating slots in which we will connect to the corresponding signals to. In our case we will be creating two slots, one to connect with the QPushButton clicked signal, and second to connect with the QHttp’s done(bool) signal to handle the response, then we declare a private QHttp member which will be sending post requests to the tinyurl’s service:

1
2
3
4
5
private slots:
    void responseFromTinyUrl();
    void shortenUrlClicked();
private:
    QHttp http;

In the above code snippet I’ve written the main parts of a header that I will handle in this blog post.

Now back to our source file, in the constructor I will set the host of the QHttp instance to www.tinyurl.com, which is very simple, just have to call the setHost(QString) function,

1
http.setHost("www.tinyurl.com");

after setting the host on the constructor we connect our slots with corresponding signals, suppose that I already have a QPushButton object instance called buttonShorten,

1
2
connect(buttonShorten, SIGNAL(clicked()), this, SLOT(shortenUrlClicked()));
connect(&http, SIGNAL(done(bool)), this, SLOT(responseFromTinyUrl()));

the implementation of the shortenUrlClicked slot is done by calling a post(QString, QByteArray) function to send a post request to the server,

1
2
3
4
void ShortenURLDialog::shortenUrlClicked()
{
     http.post(QString("api-create.php?url=%1").arg("very long url here"), QByteArray());
}

then we will handle the response that is the actual shortened URL from tinyurl.com service on the responseFromTinyUrl() function, in which we call the readAll() function to read the whole response because there will be only the shortened url string which we will display it on a simple QMessageBox,

1
2
3
4
void ShortURLDialog::responseFromTinyUrl()
{
    QMessageBox::information(this, "Shortened URL", http.readAll());
}

Conclusion
You can use any other service like tinyurl.com (for example is.gd), just replace the host and post functions to the service’s API. You can see an example of how to update the Twitter status using the same method explained in my earlier post.

blog comments powered by Disqus

874 views

© Copyright Phalanx Blogosphere - Powered by Wordpress - Phalanx logo is designed by Leopard Cana