Flylib.com

Books Software

 
 
 

Section 15.2. QMetaObject: The MetaObject Pattern


15.2. 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 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.



15.3. Type Identification and qobject_cast

RTTI , or Run-Time Type Identification , as its name suggests, is a system for determining at runtime the actual type of an object, to which we may only have a base class pointer.

In addition to C++'s RTTI operators, dynamic_cast and typeid (Section 19.8), Qt provides two mechanisms for run-time type identification.

  1. qobject_cast

  2. QObject::inherits()

qobject_cast is an ANSI-style typecast operator (Section 19.7). ANSI typecasts look a lot like template functions:


DestType

* qobject_cast<

DestType

*> ( QObject * qoptr )


A typecast operator converts an expression from one type to another, following certain rules and restrictions imposed by the types and the language. Like other cast operators, qobject_cast takes the destination type as a template parameter. It returns a different-typed pointer to the same object. If at runtime the actual pointer type cannot be converted to DestType* , then the conversion fails and the value returned is NULL .

As the signature suggests, qobject_cast is type-restricted to arguments of type DestType * , where DestType is derived from QObject and the class was fully processed by moc . Therefore, qobject_cast is actually a downcast operator, similar to dynamic_cast .

In situations where you have base class pointers to derived class objects, downcasting makes it possible to call derived class methods that do not exist in the base class interface. In Example 15.1, we take advantage of the fact that QApplication , and MyApplication , both derive from QObject .

Example 15.1. src/qtrtti/myapp-classdef.cpp
class MyApplication : public QApplication {
  Q_OBJECT   /* Required for Qt RTTI */
  public:
    static MyApplication* instance();
    QString imagesURL() const;
    [... other members ...]
};

MyApplication* MyApplication::instance() {
    static MyApplication* inst = 0;
    if (inst == 0) {
        inst = qobject_cast<MyApplication*>(qApp);
    }
    return inst;
}

Because qApp always points to the currently running QApplication , this function will return non-zero only if a MyApplication is the current running application. The downcast operation, which some say is expensive, happens only once , to ensure a properly typed MyApplication pointer. Future calls to instance() will return the previously cast pointer, avoiding repeated calls to expensive runtime checking operations. Now it becomes possible to obtain the properly typed MyApplication instance from other locations in the code:

QString imagePath(QString filename) {
    MyApplication* app = MyApplication::instance();
    QString path = app->imagesURL() + "/" + filename;

<-- 1

return path;
}


(1) The same file separator char works on all operating systems that support Qt!

Note

The implementation of qobject_cast makes no use of C++ RTTI. The code for this operator is generated by the MetaObject Compiler (moc) .


QObject also offers a Java-style typechecking function, inherits() . Unlike qobject_cast , inherits() accepts a char* type name instead of a type expression. This operation is slower than qobject_cast because it requires an extra hashtable lookup, but it can be useful if you need input-driven type checking.

Example 15.2 shows some client code that uses inherits() .

Example 15.2. src/qtrtti/qtrtti.cpp
[ . . . . ]

//  QWidget* w = &s;

<-- 1

if (w->inherits("QAbstractSlider"))  cout << "Yes, it is ";
    else cout << "No, it is not";
    cout << "a QAbstractSlider" << endl;

    if (w->inherits("QListView")) cout << "Yes, it is ";
    else  cout << "No, it is not ";
    cout << "a QListView" << endl;

    return 0;
}

(1) pointer to some widget