tags:

views:

171

answers:

3

I'm trying to compile the basic tutorial program at http://doc.trolltech.com/4.4/mainwindows-application.html and running into a problem.

Doing things the way the tutorial program does them, gives a compile error:

In file included from debug\moc_mainwindow.cpp:10:
debug\../mainwindow.h:2: error: expected class-name before '{' token
debug\../mainwindow.h:5: error: ISO C++ forbids declaration of `Q_OBJECT' with no type

The problem is the need for

#include <QtGui>

The tutorial program puts this in mainwindow.cpp which is the desirable way to do it, but then it doesn't get copied into the moc file.

If I put it into mainwindow.h instead, everything works fine, but doing that in every header file is inelegant and will extend compile times once the program gets large.

Is this something that changed since the tutorial was written? (I'm using the latest Qt 4.5.3, Windows SDK install, compiling from the command line.) Am I missing something, or is there any known fix for this problem?

+1  A: 

Your header file has to know about Qt stuff. So there is no way to avoid including QtGui.

Edit: You shouldn't worry too much about compilation time. The inclusions will happen anyways. Maybe you can split your header into none Qt-related parts if it really gets annoying.

Tristram Gräbener
Fair enough, I can live with it. Thanks!
rwallace
+5  A: 

Including #include <QMainWindow> in the header should be enough. And that is needed anyway, because QMainWindow is the base class of the class declared in mainwindow.h

Using everything-and-the-kitchen-sink headers is not a good idea even in cpp files (besides simple sample/test programs of course), because they slow down compilation unnecessarily.

Steffen
+1  A: 

sometimes i find the moc files get stale, or more likely, don't get generated in the first place. usually fixed with a full cleanup and rebuild of the project.

when using QtCreator, this happens mostly when at first i didn't put the Q_OBJECT macro, but added it later. it seems that the qmake step don't re-check if it has to pass the file through moc.

Javier