Sunday, May 9, 2010

Hidden Features of Qt

OK, the title of this post is a bit of a misnomer as the Qt documentation is so good you'd be hard pushed to find something that is truly hidden ... but I came across a great list on stackoverflow and I thought I'd share the best bits here.
  • forever
    This is a macro that expands to for (;;). Very useful. You're already using foreach
    already right?
  • qChecksum
    This returns a CRC-16 checksum of a char* buffer.
  • qCompress / qUncompress
    These functions perform zlib compression/uncompression of a QByteArray. You can also specify the compression level.
  • qFuzzyCompare
    Use this to safely compare float/double values and avoid the dreaded floating point comparison trap.
  • qVersion
    Get the runtime version of Qt being used.
  • qPrintable
    Converts a QString to a const char* instead of writing QString.toLocal8Bit().constData(). Very useful - I could of done with this the other day when outputting some debug strings using...
  • qDebug
    Write to the debug window, in std::iostream style, e.g.:

    qDebug() << "String contents: " << qPrintable(myString);

  • QObject::deleteLater()
    This is used to force an object to delete itself (delete this) when a signal occurs. I have used this to delete a QTcpSocket object automatically when the socket is disconnected.
There are lots more if you follow the link.

5 comments:

  1. *waves*

    Have you played with Creator 2.0b yet? They've made some nice addition and have even fixed *some* of the bugs I've raised.

    They are getting there, even if each bug/missing functionality fix costs two new useless features.

    ReplyDelete
  2. I'm planning a 2.0 beta post at some point. Just started using it today.

    ReplyDelete
  3. BTW your description of deleteLater is incorrect. It simply defers deletion of a pointer (which may or may not be a QObject) until the start of the next iteration.

    This is important because Qt will sometimes try to access objects after you have deleted them. So you defer their deletion.

    Whether or not you use deleteLater in response to a signal or not is irrelevant.

    ReplyDelete
  4. @Danny. Cheers for the deleteLater clarification. I assumed it was forcing Qt to somehow tidy up an object on your behalf.

    ReplyDelete
  5. Sorry I mean to say 'next iteration of the event loop'.

    ReplyDelete