Q_OBJECT and moc: A Checklist

Q_OBJECT and moc A Checklist

QObject supports features not normally available in C++ objects.

  • Children (see the first two sections of Chapter 9)
  • Signals and slots (see Section 9.3.3)
  • MetaObjects, metaproperties, metamethods (see Chapter 15)
  • qobject_cast (see Section 15.3)

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 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[5] when moc is not able to find or process your classes. To help ensure that moc processes each of your QObject-derived classes, here are some guidelines for writing C++ code and qmake project files.

[5] confusing + obfuscated

  • Each class definition should go in its own .h file.
  • Its implementation should go in a corresponding .cpp file.
  • The header file should be "#ifndef wrapped" to avoid multiple inclusion.
  • Each source (.cpp) file should be listed in the SOURCES variable of the project file, otherwise it will not be compiled.
  • The 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, so that moc will know to generate code for it.

Multiple Inheritance and QObject

Because each Q_OBJECT has signals and slots, it needs to be preprocessed by moc.moc works under the assumption that you are only deriving from QObject once, and further, that it is the first base class in the list of base classes. If you accidentally inherit from QObject multiple times, or if it is not the first base class in the inheritance list, you may receive very strange errors from moc-generated code.


Part I: Introduction to C++ and Qt 4

C++ Introduction

Classes

Introduction to Qt

Lists

Functions

Inheritance and Polymorphism

Part II: Higher-Level Programming

Libraries

Introduction to Design Patterns

QObject

Generics and Containers

Qt GUI Widgets

Concurrency

Validation and Regular Expressions

Parsing XML

Meta Objects, Properties, and Reflective Programming

More Design Patterns

Models and Views

Qt SQL Classes

Part III: C++ Language Reference

Types and Expressions

Scope and Storage Class

Statements and Control Structures

Memory Access

Chapter Summary

Inheritance in Detail

Miscellaneous Topics

Part IV: Programming Assignments

MP3 Jukebox Assignments

Part V: Appendices

MP3 Jukebox Assignments

Bibliography

MP3 Jukebox Assignments



An Introduction to Design Patterns in C++ with Qt 4
An Introduction to Design Patterns in C++ with Qt 4
ISBN: 0131879057
EAN: 2147483647
Year: 2004
Pages: 268

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net