[ fromfile: qobject.xml id: moc ]
QObject supports features not normally available in C++ objects.
Signals and Slots (Section 8.5)
MetaObjects, MetaProperties, MetaMethods (Chapter 12)
qobject_cast
(Section 12.2)
Some of these features are only possible through the use of generated code.
The Meta Object Compiler, moc, generates additional functions for each QObject-derived class that uses the
Q_OBJECT macro.
Generated code can be found in files with names moc_filename.cpp
.
This means that some errors from the compiler/linker may be confuscated[67] when moc cannot find or process a class in the project. To help ensure that moc processes each QObject-derived class in the project, following are some guidelines for writing C++ code and qmake project files.
Each class
definition should go in its own .h
file.
Its implementation should go in a corresponding .cpp
file.
The header file should be "wrapped" (e.g., with #ifndef
) to avoid multiple
inclusion.
Each .cpp
file should be listed in the SOURCES
variable of the project file; otherwise it will not be compiled.
Each header file should be listed in the HEADERS
variable of the .pro
file. Without this, moc will not preprocess the file.
The Q_OBJECT
macro must appear inside the class
definition of each QObject derived header file so
that moc knows to
generate code for it.
Multiple Inheritance and QObject | |
---|---|
Because each Q_OBJECT macro generates code, it needs to be preprocessed by moc. moc works under the assumption that you are only deriving from QObject once and, further, that QObject is the first base class in the list of base classes. If you accidentally inherit from QObject more than once, or if it is not the first base class in the inheritance list, you may produce strange errors in the moc-generated code. |
Undefined reference to vtable | |
---|---|
If you define a QObject-derived class, build
an application, realize that you need to add the |
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |