Friday, June 22, 2012

Useful Tricks in QT


  • qPrintable(QString str) - skips the several function calls it usually takes to get to a standard char*
  • .pro file basics
    • Need to include a header from a new directory?
      • INCLUDEPATH += /path/to/directory/
      • HEADERS += myheader.h
      • #include <myheader.h> in the file you are working on.
      • For some reason, it took a while for the header to be recognized. I reran qmake and tried rebuilding a few times. This may not always happen.
        • update: i think the reason the header was not recognized was that I had misspelled it. Whoops! Once INCLUDEPATH and HEADERS were updated, the header appeared in the autocomplete.
  • Random Numbers
    • call the following first and only once: qsrand(QDateTime::currentDateTime().toTime_t());
    • to get a random number 0..max, do something like the function below
    • qint32 randNum(qint32 max)
      {
      return ((qreal)qrand())/RAND_MAX * max;
      }
      
      

No comments:

Post a Comment