Perhaps one of the most often used widgets in Qt is QLabel. It is mainly used to display a plain text as well as rich one (which contains HTML markup). QLabel is also able to show graphics by passing QPixmap instance to it. But what if there is a need to display (using QLabel) a pixmap stored on remote server? There is no such "out of the box" functionality in QLabel. But it can be easily done. Here I want to show you how.
We just make a derivative class of QLabel and add only one new public method -- setRemotePixmap(const QString&) to let the label know that we want to display a remote graphics. Then the label composes HTTP GET request using QNetworkAccessManager class to retrieve the file. When the image is retrieved, it is showed up using setPixmap(const QPixmap&).
Let's see the source code.
Showing posts with label qt. Show all posts
Showing posts with label qt. Show all posts
Thursday, March 11, 2010
Code snippet: QLabel to show remote pixmap by URL
Labels:
code snippet,
qt
Friday, February 26, 2010
QCryptographicHash: code snippet
When the guys developing Qt applications find themselves in such a case when they need to obtain MD5 hash of a string or an array of bytes, the first thing most of them do is try to use the already implemented 3rd-party libraries like libxcrypt, OpenSSL and others. The talented ones try to get the output of 'md5sum' command.
But the 'true' way is to use built-in tools: not everyone knows that Qt already has QCryptographicHash class which can help us. Let's see how:
QString password("our super secret password");
QByteArray hashed_password_ba(
QCryptographicHash::hash(password.toAscii(),
QCryptographicHash::Md5));
/* Now 'md5_password' var contains
* md5-hashed our super secret password
*/
QString md5_password(hashed_password_ba.toHex().constData());
Very easy, isn't it? Apart from MD5, the class allows to generate MD4 and Sha1 hashes which may also be very helpful in some special cases.
But the 'true' way is to use built-in tools: not everyone knows that Qt already has QCryptographicHash class which can help us. Let's see how:
QString password("our super secret password");
QByteArray hashed_password_ba(
QCryptographicHash::hash(password.toAscii(),
QCryptographicHash::Md5));
/* Now 'md5_password' var contains
* md5-hashed our super secret password
*/
QString md5_password(hashed_password_ba.toHex().constData());
Very easy, isn't it? Apart from MD5, the class allows to generate MD4 and Sha1 hashes which may also be very helpful in some special cases.
Labels:
code snippet,
qt
Subscribe to:
Posts (Atom)