Wednesday, May 27, 2009

Qt Puzzle (answer part)

(continued from phosphorescence: Qt Puzzle (question part))

Difference between pattern 1 and pattern 2 is #include declaration. Entire QtGui module is included in pattern 1, and necessary Qt classes are included in pattern 2 one by one.

What happens by this difference? A compile error like below occurs.

log10 method is not found. log10 method is declared on <cmath>, and probably, some class in QtGui module includes it. But, it isn't QApplication class or QLabel class. So pattern 2 need to include <cmath> explicitly like below.
#include <QApplication>
#include <QLabel>
#include <cmath>

int main(int argc, char *argv[])
QApplication a(argc, argv);
int source_number = 100;
QString message = "log10("
+ QString::number(source_number)
+ ") = "
+ QString::number(log10(source_number));
QLabel *label = new QLabel(message);
return a.exec();

Then, compile and run successfully. This dialog appears.

No comments:

Post a Comment