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(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


Q_PROPERTY Macro Describing QObject Properties

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