Behind Twithor – Updating Twitter status message using Qt
Written on May 22, 2009, by Milot Shala.

In my earlier post, I’ve introduced to you Twithor, which is a small application that allow users to update their status message on twitter. In this post I will explain Twithor under the hood, how to implement this feature to your application.

For this kind of problem, we will be using QHttp class which provides the HTTP Request functionality, and we will be using Twitter API to send an http request via POST method.

Now we need to create an instance of QHttp class:

1
QHttp *twitter_http_updater = new QHttp();

QHttp class provides methods for sending http request to the server, but first we need to tell in which host to send the message, we will need to tell the username and password for the message because the Twitter system is not that intelligent to know that you are sending a message without knowing your credentials and in the end as I mentioned above we need to use twitter api to send the message.

Code means a thousands words

Signatures of the functions that we will be using are:

1
2
3
4
5
6
7
8
// for setting the hostname
QHttp::setHost ( const QString & hostName, quint16 port = 80 )
 
// for setting the username and password
QHttp::setUser ( const QString & userName, const QString & password = QString() )
 
// for sending the request to the server
QHttp::post ( const QString & path, const QByteArray & data, QIODevice * to = 0 )

As seen in the code above QIODevice optional value of the parameter is set to 0 and if we don’t use that parameter it will be 0, but in the other hand QByteArray reference variable need to be passed and we pass the empty QByteArray() because we are using only path parameter to send the data with the request as seen in the code below:

1
2
3
twitter_http_updater->setHost("www.twitter.com");
twitter_http_updater->setUser("yourusername", "yourpassword");
twitter_http_updater->post("/statuses/update.xml?status=mybeautifulmessage", QByteArray());

One more thing for the end, in Twithor I am handling done signal by a slot. Done signal is emitted when the request was sent and the response is received in other words when the communication is finished between the host and your client, done signal has the following signature:

1
QHttp::done ( bool error )   [signal]

I connected this signal to a slot nicely:

1
connect(twitter_http_updater, SIGNAL(done(bool)), this, SLOT(updateTwitterStatusFinish()));

And this slot will be fired when the signal is emitted and I’ve implemented the the slot to tell the user that his or her status is updated, you can see more features by reading the docs here.

  • http://feedmytwitter.com srdha

    i gust want to say some thing “great job”

    Update your Twitter randomly according to your intrest Or, from Rss Feed Or, from your own tweet message list Or, Any combination of the above three http://feedmytwitter.com

  • http://codespartan.org/blog/2009/07/shorten-your-urls-using-qt/ Milot Shala’s Blog » Shorten your URLs using Qt

    [...] API. You can see an example of how to update the Twitter status using the same method explained in my earlier post. Leave a comment | Trackback No comments [...]

  • http://www.ktoon.net Gustav Gonzalez

    Hello Milot!

    I wonder if the twithor source code is already available from some url. Is it?

    I appreciate if you let me know. Thanks!

  • http://spartansoft.org Milot Shala

    @Gustav,

    I will make it available ASAP ;)

blog comments powered by Disqus

1,702 views

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