4

I just started working with Qt (in C++), so I followed a "hello, world" example I found online. I created the program hello.cpp in the directory hello:

#include <QtGui>

int main(int argc, char *argv[]) {
    QApplication app (argc, argv);
    QLabel label ("Hello, world!");
    label.show();
    return app.exec();
}

I ran:

qmake -project
qmake hello.pro
make

and everything compiled correctly and I was able to run ./hello. Then, being an adventurous person, I tried modifying the file:

#include <QtGui>
#include <QtWebKit>

int main(int argc, char *argv[]) {
    QApplication app (argc, argv);
    QLabel label ("Hello, world!");
    QWebPage page;
    label.show();
    return app.exec();
}

I reran the three commands, but now when I run make I get the following error:

hello.cpp:2: fatal error: QtWebKit: No such file or directory
compilation terminated.
make: *** [hello.o] Error 1

I checked out the Makefile and the INCPATH variable was defined as

INCPATH = -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I.

It is noticeably missing -I/usr/include/qt4/QtWebKit. The LIBS variable was also missing -lQtWebKit. Adding these manually causes the compilation to succeed. What am I doing wrong? What do I need to add to make qmake generate the correct Makefile?

larsmoa
  • 12,604
  • 8
  • 62
  • 85
Nick
  • 2,821
  • 5
  • 30
  • 35

1 Answers1

8

You need to add:

QT += webkit

to your .pro file, and re-run qmake.

qmake -project does not try to guess what modules you need in your code.

If you have more than one module, the usual syntax is like:

QT += webkit xml network
Mat
  • 202,337
  • 40
  • 393
  • 406
  • Thanks. Is "QT += webkit" the standard format? If I wanted to add QtXml too would I put "QT += xml" as well, or "QT += webkit, xml"? – Nick Jul 10 '11 at 18:56