[ fromfile: reflection-questions.xml id: reflection-questions ]
What kinds of information can you obtain from a QMetaObject?
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
method(index)
, which returns the meta-data for the method with the given index.
propertyCount()
, which returns the number of properties in this class, including base class properties.
property(index)
, which returns the meta-data for the property with the given index.
What Qt classes are used to do data reflection? Explain why.
QMetaObject, QMetaProperty, QSqlDatabase::tables, QSqlRecord, QVariant
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.
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.
What is RTTI? How does Qt provide RTTI?
Discuss the differences between the dynamic_cast
and qobject_cast
operators.
What is a QVariant? How might you make use of one?
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.
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.
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
Explain how dynamic properties can be serialized.
They are stored in a QVariantMap, which can be serialized via a QDataStream.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |