12.9.  Review Questions

[ fromfile: reflection-questions.xml id: reflection-questions ]

  1. What kinds of information can you obtain from a QMetaObject?

     

    1. className(), which returns the class name as a const char*

    2. superClass(), which returns a pointer to the QMetaObject of the base class if there is one (or 0 if there is not)

    3. methodCount(), which returns the number of member functions of the class

    4. method(index), which returns the meta-data for the method with the given index.

    5. propertyCount(), which returns the number of properties in this class, including base class properties.

    6. property(index), which returns the meta-data for the property with the given index.

  2. What Qt classes are used to do data reflection? Explain why.

      QMetaObject, QMetaProperty, QSqlDatabase::tables, QSqlRecord, QVariant

  3. How does the QMetaObject code for each of your QObject-derived classes get generated?

      moc generates QMetaObject classes to support properties, signals and slots. Normally, you do not run moc directly. It is run automatically by make on the header files listed in HEADERS which use the Q_OBJECT macro.

  4. What is a downcast? In what situations do you use them?

      A downcast is a conversion from something "higher" (more general) in the inheritance graph, to a "lower" class (one of its derived types). We use downcasts in situations where we are forced to use a base class pointer (usually because a function we did not write is using that type) but we require a non-polymorphic derived class member function.

  5. What is RTTI? How does Qt provide RTTI?

  6. Discuss the differences between the dynamic_cast and qobject_cast operators.

  7. What is a QVariant? How might you make use of one?

  8. What are the advantages of using property() and setProperty() over direct getters and setters?

      Q_PROPERTY macros make it possible for moc to generate code for QObject's property() and setProperty() member functions. The advantage of using these functions is that client code can determine which properties are available by iterating through QMetaProperty objects of the QMetaObject corresponding to that class.

  9. What does the property() function return? How do you obtain the actual stored value?

      property() returns a QVariant, which is a union wrapper around every possible basic type, and also several Qt classes/types. With QVariant, you can ask for its type() and convert to the actual value<>(). Benefits are most apparent when implementing script engines or developer tools. It becomes possible to define "handle anything" kinds of functions without using anything but the QObject interface to read and write values.

  10. Explain how it is possible to add new properties, acquired at runtime, to a QObject.

      setProperty("propName") sets a dynamic property for that object even if it is not declared as a Q_PROPERTY

  11. Explain how dynamic properties can be serialized.

      They are stored in a QVariantMap, which can be serialized via a QDataStream.