Re: Problem with the Qt docs
Your thread subject is "Problem with the Qt docs." I actually found something in the docs the other day when trying to find an answer for somebody else that was like, "We cannot stress enough that the most common bug people find in their software is an error about missing vtables. This 99% of the time means you forgot to run the moc on your code."
I can't seem to find it now, but I found this instead:

Originally Posted by
Qt 4.1 Reference
Diagnostics
moc will warn you about a number of dangerous or illegal constructs in the Q_OBJECT class declarations.
If you get linkage errors in the final building phase of your program, saying that YourClass::className() is undefined or that YourClass lacks a vtable, something has been done wrong. Most often, you have forgotten to compile or #include the moc-generated C++ code, or (in the former case) include that object file in the link command. If you use qmake, try rerunning it to update your makefile. This should do the trick.
Why would a header/implementation separation solve the problem? Or is that just a magic MOC thing that I have to accept?
Because MOC doesn't look at your .cpp files for Q_OBJECT unless you tell it to. Under normal circumstances it only cares about the .h files. From the manual for MOC, however, is the following:
For Q_OBJECT class declarations in implementation (.cpp) files, we suggest a makefile rule like this:
foo.o: foo.moc
foo.moc: foo.cpp
moc -i $< -o $@
This guarantees that make will run the moc before it compiles foo.cpp. You can then put
#include "foo.moc"
at the end of foo.cpp, where all the classes declared in that file are fully known.
So it's not impossible, it's just not as easy as keeping your declarations separate from the implementation and doing "qmake -project ; qmake ; make." I think actually in many of the examples with the docs, there is a piece of code in main.cpp that says "#include main.moc" at the bottom. Is it in your Counter example or no? That is what this line means.
My philosophy is: If you can use a free, open-source alternative: do it. And if you can't, pretend it's free and open-source and hope you don't get caught.
Bookmarks