QMetaObject: The MetaObject Pattern

QMetaObject The MetaObject Pattern

By abstracting the abstract data type itself, we achieve what is called a MetaObject. A MetaObject is an object that describes the structure of another object.[2]

[2] Meta, the latin root meaning about, is used for its literal definition here.

The MetaObject Pattern

The QMetaObject is Qt's implementation of the MetaObject pattern. It provides information about properties and methods of a QObject. The MetaObject pattern is sometimes known as the Reflection pattern.

A class that has a MetaObject supports reflection. This is a feature found in many object-oriented languages. It does not exist in C++, but Qt's MetaObject compiler (moc) generates the code to support this for desired classes.

As long as certain conditions apply,[3] each class derived from QObject will have a QMetaObject generated for it by moc, as shown in Figure 15.2. QObject has a member function that returns a pointer to the object's QMetaObject.

[3] Each class must be defined in a header file, listed in the project file's HEADERS, and must include the Q_OBJECT macro in its class definition.

Figure 15.2. MetaObjects

QMetaObject* QObject::metaObject () const [virtual]

A QMetaObject can be used to invoke functions such as:

  • className(), which returns the class name as a const char*
  • superClass(), which returns a pointer to the QMetaObject of the base class if there is one (or 0 if there is not)
  • methodCount(), which returns the number of member functions of the class
  • Several other useful functions that we will discuss in this chapter

The signal and slot mechanism also relies on the QMetaObject.

By using the QMetaObject and QMetaProperty, it is possible to write code that is generic enough to handle all self-describing classes.


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